【发布时间】:2014-12-08 17:21:25
【问题描述】:
我正在使用动作脚本 3 制作射击游戏。我有一个英雄,当按下任何箭头键时都会移动 我的代码如下
//some class level variables
private var vx :int = 0;
private var vy :int = 0;
//in the Main constructor
stage.addEventListener(KeyboardEvent.KEY_DOWN , moveHero);
stage.addEventListener(KeyboardEvent.KEY_UP , stopHero);
stage.addEventListener(Event.ENTER_FRAME , onEnter);
//and all the handlers
function moveHero(e:KeyboardEvent)
{
if (e.keyCode == 37)
{
vx = -5;
}
else if (e.keyCode == 38)
{
vy = -10;
}
else if (e.keyCode == 39)
{
vx = 5;
}
else if (e.keyCode == 40)
{
vy = 10;
}
}
function stopHero(e:KeyboardEvent)
{
//when key is up stop miving the hero
vx = 0;
vy = 0;
}
function onEnter(e:Event):void
{
//updtae hero position
hero.x += vx;
hero.y += vy;
}
现在我的问题是,当用户在他的手指下同时有上下键或左右键并突然交替按下它们时,英雄在对按键的响应方面表现出明显的滞后
【问题讨论】:
-
按键应该设置布尔值,并且在你的输入框监听器中你应该根据这些布尔值移动角色。操作本身不应发生在按键处理程序中。
-
@BotMaster 为什么 OP 需要一个 enterframe 监听器?
标签: actionscript-3 flash events