当我想用键盘移动我的播放器时,一旦我放开箭头键,播放器就会继续移动。 (AS3)

时间:2016-05-30 13:04:30

标签: actionscript-3

public function keyD(p:KeyboardEvent):void
{
    trace(p.keyCode);
    //a=65  d=68 w=87 s=83 
    //to program the 4 arrow keys
    if (p.keyCode==Keyboard.LEFT)
    {
        vx=-5;
    }
    else if (p.keyCode==Keyboard.RIGHT)
    {
        vx=5;
    }
    else if (p.keyCode==Keyboard.UP)
    {
        vy=-5;
    }
    else if (p.keyCode==Keyboard.DOWN)
    {
        vy=5;
    }
}
public function keyU(e:KeyboardEvent):void
{
    //we want to program the 4 arrow keys
    if (p.keyCode==Keyboard.LEFT) //Make player move left
    {
        vx=0;
    }
    else if (p.keyCode==Keyboard.RIGHT) //Make player move right
    {
        vx=0;
    }
    else if (p.keyCode==Keyboard.UP) //Make player move up
    {
        vy=0;
    }
    else if (p.keyCode==Keyboard.DOWN) //Make player move down
    {
        vy=0;
    }
}



public function gameLoop(e:Event):void
{
    //this function is called 24 times each second

    p.x+=vx;//move the player horizontally
    p.y+=vy;//move the player vertically

1 个答案:

答案 0 :(得分:4)

keyU中的参数名为e,而不是p,因此您的switch-case运算符会将null与某些内容进行比较。将e替换为p或将所有p替换为e s。