Actionscript 3 AS3:仅按单键
我正在尝试创建一个平台游戏,我有跑,跳和双跳工作。现在我的问题是,我希望玩家按下跳跃按钮,英雄只跳跃一次,而不是连续跳跃。如果我按住“向上”按钮,它将继续跳跃,这是我不想要的,我只想让它执行一次跳跃,即使按键仍被按住。我想要同样的双人跳远,我怎样才能做到这一点Actionscript 3 AS3:仅按单键,actionscript-3,keypress,Actionscript 3,Keypress,我正在尝试创建一个平台游戏,我有跑,跳和双跳工作。现在我的问题是,我希望玩家按下跳跃按钮,英雄只跳跃一次,而不是连续跳跃。如果我按住“向上”按钮,它将继续跳跃,这是我不想要的,我只想让它执行一次跳跃,即使按键仍被按住。我想要同样的双人跳远,我怎样才能做到这一点 package { import flash.display.MovieClip; import flash.events.Event; import flash.events.KeyboardEvent;
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Player extends MovieClip
{
//Player run speed setting
var RunSpeed:Number = 8;
//Player key presses
var RightKeyPress:Boolean = false;
var LeftKeyPress:Boolean = false;
var UpKeyPress:Boolean = false;
//Jump variables
var Gravity:Number = 1.5;
var JumpPower:Number = 0;
var CanJump:Boolean = false;
var DoubleJump:Boolean = false;
public function Player()
{
// constructor code
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyPressed);
addEventListener(Event.ENTER_FRAME,Update);
stage.addEventListener(KeyboardEvent.KEY_UP,KeyReleased);
}
function KeyPressed(event:KeyboardEvent)
{
//When Key is Down
if (event.keyCode == 39)
{
RightKeyPress = true;
}
if (event.keyCode == 37)
{
LeftKeyPress = true;
}
if (event.keyCode == 38)
{
UpKeyPress = true
}
}
function Update(event:Event)
{
//Adding gravity to the game world
JumpPower += Gravity;
//if player is more than 300 on the y-axis
if (this.y > 300)
{
//Player stays on the ground and can jump
JumpPower = 0;
CanJump = true;
DoubleJump = false;
}
//If player is on floor and right key is pressed then run right
if ((RightKeyPress && CanJump))
{
x += RunSpeed;
gotoAndStop('Run');
scaleX = 1;
}
else if ((LeftKeyPress && CanJump))
{
//Otherwise if on floor and left key is pressed run left
x -= RunSpeed;
gotoAndStop('Run');
scaleX = -1;
}
else if ((UpKeyPress && CanJump))
{
//Otherwise if on floor and up key is pressed then jump
JumpPower = -15;
CanJump = false;
gotoAndStop('Jump');
DoubleJump = true;
}
//If on floor and right and up key are pressed then jump
if ((RightKeyPress && UpKeyPress && CanJump))
{
JumpPower = -15;
CanJump = false;
gotoAndStop('Jump');
DoubleJump = true;
}
//If on floor and left and up key are pressed then jump
if ((LeftKeyPress && UpKeyPress && CanJump))
{
JumpPower = -15;
CanJump = false;
gotoAndStop('Jump');
DoubleJump = true;
}
//If jumped and right key is pressed then move right
if ((RightKeyPress && CanJump == false))
{
x += RunSpeed;
scaleX = 1;
}
else if ((LeftKeyPress && CanJump == false))
{
//Otherwise if jumped and left key is pressed then move left
x -= RunSpeed;
scaleX = -1;
}
//If in air and able to doublejump and pressed up key, then double jump
if (UpKeyPress && DoubleJump && JumpPower > -2)
{
JumpPower = -13;
DoubleJump = false;
gotoAndStop('DoubleJump');
}
//If on floor and no key is presses stay idle
if ((!RightKeyPress && !LeftKeyPress && CanJump))
{
gotoAndStop('Idle');
}
this.y += JumpPower;
}
function KeyReleased(event:KeyboardEvent)
{
if (event.keyCode == 39)
{
event.keyCode = 0;
RightKeyPress = false;
}
if (event.keyCode == 37)
{
event.keyCode = 0;
LeftKeyPress = false;
}
if (event.keyCode == 38)
{
event.keyCode = 0;
UpKeyPress = false;
}
}
}
}
我怀疑发生的情况是,一旦你的玩家的y值满足条件
如果(this.y>300)
你允许他再次跳跃,那么只要按住键,他就会跳跃,因为UpKeyPress&&CanJump
都是真的
我的猜测是这样做可能会让你更接近你的答案
在更新功能中:
function Update(event:Event)
{
//Adding gravity to the game world
JumpPower += Gravity;
//if player is more than 300 on the y-axis
if (this.y > 300)
{
//Player stays on the ground and can jump
JumpPower = 0;
// Do not allow another jump until the UpKey is pressed
//CanJump = true;
//DoubleJump = false;
}
...
function KeyPressed(event:KeyboardEvent)
{
//When Key is Down
if (event.keyCode == 39)
{
RightKeyPress = true;
}
if (event.keyCode == 37)
{
LeftKeyPress = true;
}
if (event.keyCode == 38)
{
UpKeyPress = true
// Do not allow another jump until the UpKey is pressed
if (this.y > 300)
{
CanJump = true;
DoubleJump = false;
}
}
}
然后在按键功能中:
function Update(event:Event)
{
//Adding gravity to the game world
JumpPower += Gravity;
//if player is more than 300 on the y-axis
if (this.y > 300)
{
//Player stays on the ground and can jump
JumpPower = 0;
// Do not allow another jump until the UpKey is pressed
//CanJump = true;
//DoubleJump = false;
}
...
function KeyPressed(event:KeyboardEvent)
{
//When Key is Down
if (event.keyCode == 39)
{
RightKeyPress = true;
}
if (event.keyCode == 37)
{
LeftKeyPress = true;
}
if (event.keyCode == 38)
{
UpKeyPress = true
// Do not allow another jump until the UpKey is pressed
if (this.y > 300)
{
CanJump = true;
DoubleJump = false;
}
}
}
我还支持shaunhusain的建议,即设置断点并熟悉调试代码。您是否尝试过在双跳转发生的位置设置断点?在我看来,布尔逻辑应该是正确的,但你一定是因为某种原因反复出现在那个块?尝试在更新函数中的关键点追踪布尔值。@shaunhusain:Break point?我对AS3有点陌生,所以我知道的还不多,但我学得很快。没问题,检查一下这个链接,或者用谷歌搜索一下,这篇文章的后半部分可能会有帮助,但它很长。为了将来的参考,有一个键盘类列举了一些键,所以你可以用Keyboard.UP替换38。