如何用 1 键将物体移动固定距离
how to move an object a fixed distance with 1 press
这是一个非常简单的问题,但在 google 中尝试了几次之后,我仍然无法解决这个问题。
我有一个英雄,如果用户按住按钮并每帧改变位置 5,它会左右走动。
现在我想要的是,如果我按一次键(不按住),那么它通常会 go/walk 到固定距离(比如说 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");
}
}
设置另一个属性:
var counter: uint = 0;
像这样在你的 gameLoop 函数中放置一个计数器:
if (dPressed == true) {
counter++;
hero.x += 5;
hero.goToAndStop("hero Move Right");
if (counter >= 10){
dPressed = false;
counter = 0;
}
}
这是一个非常简单的问题,但在 google 中尝试了几次之后,我仍然无法解决这个问题。
我有一个英雄,如果用户按住按钮并每帧改变位置 5,它会左右走动。
现在我想要的是,如果我按一次键(不按住),那么它通常会 go/walk 到固定距离(比如说 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");
}
}
设置另一个属性:
var counter: uint = 0;
像这样在你的 gameLoop 函数中放置一个计数器:
if (dPressed == true) {
counter++;
hero.x += 5;
hero.goToAndStop("hero Move Right");
if (counter >= 10){
dPressed = false;
counter = 0;
}
}