-1

I am trying to use a variable in my switch statement as it makes the player movement smooth. This is my smooth player movement code.

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;}
}

I am trying to implement it into my existing code. i need the switch statements for collision.

This code works fine but player movement is very slugish.

Thanks in advance

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;
}

}

4

2 に答える 2

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 ;
}

注: ヒーローをスムーズに動かしたい場合は、ゲームのフレーム レートを上げて速度を下げます。そのため、ヒーローは、少ないステップではなく、スムーズな動きのように見えるより小さなステップを実行ます、大きなステップを実行します。

したがって、+ 10 インクリメントする代わりに、+ 2 にしてフレーム レートを上げます。滑らかな動きには 30 ~ 40 fps で十分です。

幸運を祈ります、それが役立つことを願っています:)

于 2012-04-25T10:16:43.720 に答える
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;
    }
}
于 2012-04-24T06:55:45.007 に答える