什么是在Flash游戏中检测持有按键的正确方法?例如,我想知道右边的箭头是为了移动玩家。如何检测Flash游戏中的持有密钥?
朴素代码:
function handleKeyDown(event:KeyboardEvent) {
held[event.keyCode] = true;
}
function handleKeyUp(event:KeyboardEvent) {
held[event.keyCode] = false;
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, handleKeyUp);
天真的代码对某些计算机问题。 KEY_DOWN事件与KEY_UP交替多次,用于持有密钥。 这使得密钥似乎在某些帧中被释放。
中看到事件的一个例子:
[Just holding a single key.]
KEY_DOWN,KEY_UP,KEY_DOWN,KEY_UP,KEY_DOWN,KEY_UP,...
justPressed.length = 0;而不是justPressed = []。为什么当你可以重新使用旧的时候,为什么每一帧都要创建新的数组 – alxx 2010-09-13 06:55:28
@alxx谢谢。我更新了代码。 – 2010-09-13 10:04:37