【问题标题】:Keyboard code makes character jump straight up, but not move left nor right when it's in the air键盘代码使角色直接向上跳跃,但在空中时不会左右移动
【发布时间】:2014-05-11 07:34:32
【问题描述】:
public function movementChar()
{   
    if (upKey)
    {
        this.y -= 10;
        this.gotoAndStop("jump");
        //this.scaleX = -1;     
    }
    else
    if (leftKey)
    {
        this.x -= xSpeed;
        this.gotoAndStop("run");
        this.scaleX = -1;
    }
    else if (rightKey)
    {
        this.x += xSpeed;
        this.gotoAndStop("run");
        this.scaleX = 1;
    }
    else if(!leftKey || !rightKey)
    {
        this.gotoAndStop("stop");
    }
}

按住左键可以左移,按住右键可以右移, 但是当我按下时,角色会跳跃并且不会移动,但只有当我按住左键和上键||时才会向上移动右键和向上键。

如果有帮助,这里是其余的代码。

private function keyUp(e:KeyboardEvent):void 
{
    if (e.keyCode == 37)
    {
        leftKey = false;
    }
    if (e.keyCode == 39)
    {
        rightKey = false;
    }   
    if (e.keyCode == 38)
    {
        upKey = false;
    }       
}

private function keyDown(e:KeyboardEvent):void 
{
    if (e.keyCode == 37)
    {
        leftKey = true;
    }
    if (e.keyCode == 39)
    {
        rightKey = true;
    }
    if (e.keyCode == 38)
    {
        upKey = true;
    }
}

【问题讨论】:

    标签: actionscript-3 game-engine


    【解决方案1】:

    问题是你在 if/if else 条件下设置它,但它应该只是 if 条件。喜欢

    if(leftKey){
    ...
    }
    if(rightKey){
    ..
    }
    if(upKey){
    ...
    }
    

    因为考虑一下,在您的代码中,您首先检查是否按下了 upKey,如果评估结果为 true,它将执行该部分并跳过所有其余部分,因为所有其他 if 条件只能在 upKey 为 false 时执行。

    【讨论】:

    • 很好的答案,谢谢!有用。但是有一个问题。动画继续播放,应该有一个空闲动画,所以如果玩家没有按下任何东西(如果所有键都是假的),那么角色应该进入“空闲”帧。
    • if (!upKey && !leftKey && !rightKey) { this.gotoAndStop("stop"); }
    • 你可能已经知道了,但是如果你想要一个空闲动画,只需检查每个键是否为假,即 if (!upKey&& !leftKey&& !rightKey) then (play idle)
    猜你喜欢
    • 2017-12-22
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多