如何通过1按下将对象移动固定距离

时间:2016-05-11 15:16:06

标签: actionscript-3 flash

这是一个非常简单的问题,但在谷歌尝试了几次后,我仍然无法解决这个问题。

如果用户按住按钮并且每帧更改位置5,我有一个向左和向右走的英雄。

现在我想要的是,如果我按下一次按键(不按住),那么它将正常行走到固定距离(比如说x +=50)并停在那里。

这是我的代码

import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;


var dPressed:Boolean = false;
var aPressed:Boolean = false;


stage.addEventListener(KeyboardEvent.KEY_DOWN , keyDownHandaler);
stage.addEventListener(KeyboardEvent.KEY_UP , KeyUpHandaler);
stage.addEventListener(Event.ENTER_FRAME , gameLoop);

function keyDownHandaler(Devent:KeyboardEvent):void
{
        if (Devent.keyCode == Keyboard.D)
            {
                dPressed = true;
            }
        else if (Devent.keyCode == Keyboard.A)
            {
                aPressed = true;
            }
}


function KeyUpHandaler (Uevent:KeyboardEvent):void 
{
    if (Uevent.keyCode == Keyboard.D)
        {
            dPressed = false;
            hero.gotoAndStop("hero Stand");
        }
    else if(Uevent.keyCode == Keyboard.A)
    {
            aPressed = false;
            hero.gotoAndStop("hero Stand");
    }   

}

function gameLoop(Levent:Event):void
{
    if (dPressed)
        {
            hero.x += 5;
            hero.gotoAndStop("hero Move Right");
        }
    else if(aPressed)
        {
            hero.x -= 5;
            hero.gotoAndStop("heroMove Left");
        }

    }

1 个答案:

答案 0 :(得分:0)

设置另一个属性:

var counter: uint = 0;

将一个计数器放在你的gameLoop函数中:

if (dPressed == true) {
     counter++;
     hero.x += 5;
     hero.goToAndStop("hero Move Right");
     if (counter >= 10){
         dPressed = false;
         counter = 0;
     }
}