如何在switch语句中使用变量*已更新*

时间:2012-04-23 17:03:16

标签: flash actionscript

我正在尝试在switch语句中使用变量,因为它可以使播放器运动平稳。这是我平稳的玩家动作代码。

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
    stage.addEventListener(Event.ENTER_FRAME,movehero);
    function keyPressedDown(event:KeyboardEvent){
if(event.keyCode==37){leftArrow = true;}
else if (event.keyCode == 39){rightArrow=true;}
else if(event.keyCode ==38){upArrow = true;}
else if(event.keyCode ==40){downArrow = true;}
}

    function keyPressedUp(event:KeyboardEvent){
if(event.keyCode==37){leftArrow = false;}
else if (event.keyCode == 39){rightArrow=false;}
else if(event.keyCode ==38){upArrow = false;}
else if(event.keyCode ==40){downArrow = false;}
    }

    function movehero(event:Event){
if(leftArrow){hero.x -= 10;}
if(rightArrow){hero.x += 10;}
if(upArrow){hero.y -= 10;}
if(downArrow){hero.y += 10;}
}

我正在尝试将其实现到我现有的代码中。我需要switch语句来进行冲突。

此代码运行良好,但玩家移动非常紧张。

提前致谢

I am trying to acheive something like this, but this does not work 

 var leftArrow:Boolean= false;
 var rightArrow:Boolean= false;
 var upArrow:Boolean= false;
 var downArrow:Boolean=false;


    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
    stage.addEventListener(Event.ENTER_FRAME,movehero);

    function keyPressedDown(event:KeyboardEvent){
if(event.keyCode==37){leftArrow = true;}
else if (event.keyCode == 39){rightArrow=true;}
else if(event.keyCode ==38){upArrow = true;}
else if(event.keyCode ==40){downArrow = true;}
}

    function keyPressedUp(event:KeyboardEvent){
if(event.keyCode==37){leftArrow = false;}
else if (event.keyCode == 39){rightArrow=false;}
else if(event.keyCode ==38){upArrow = false;}
else if(event.keyCode ==40){downArrow = false;}
    }

    function movehero(event:Event){

switch(event){
    case leftArrow:
    hero.x -=10;
    break;

    case rightArrow:
    hero.x +=10;
    break;

    case upArrow:
    hero.y -=10;
    break;

    case downArrow:
    hero.y += 10;
    break;

    default  :
    break;
}

}

2 个答案:

答案 0 :(得分:1)

使用键盘时,设置方向的布尔变量是一个很好的模式。试试这个函数movehero的代码:

function movehero(event:Event)
{    
        if (this.leftArrow):
            hero.x -= 10 ;

        if (this.rightArrow)
            hero.x += 10 ;

        if (this.upArrow)
            hero.y -= 10 ;

        if (this.downArrow)
            hero.y += 10 ;
}

注意:如果你真的想让你的英雄顺利移动,提高游戏的帧速率,并降低速度。因此,英雄将执行更多小步骤,这似乎是平稳移动,而不是 FEWER ,而是 BIG 步骤。

因此,不是增加+ 10,而是使其+2并增加帧速率。 30-40 fps非常适合平稳移动。

祝你好运,希望有所帮助:)

答案 1 :(得分:0)

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
stage.addEventListener(Event.ENTER_FRAME,movehero);

function keyPressedDown(event:KeyboardEvent)
{
    if(event.keyCode==37) leftArrow = true;
    else if(event.keyCode == 39) rightArrow = true;
    else if(event.keyCode ==38) upArrow = true;
    else if(event.keyCode ==40) downArrow = true;
}

function keyPressedUp(event:KeyboardEvent)
{
    if(event.keyCode == 37) leftArrow = false;
    else if (event.keyCode == 39) rightArrow = false;
    else if(event.keyCode == 38) upArrow = false;
    else if(event.keyCode == 40) downArrow = false;
}

function movehero(event:Event)
{
    switch(true)
    {
        case (leftArrow):
            hero.x -=10;
            break;

        case (rightArrow):
            hero.x +=10;
            break;

        case (upArrow) :
            hero.y -=10;
            break;

        case (downArrow):
            hero.y += 10;
            break;
    }
}