"초기 지연"이 운영 체제 구성에 따라 다르므로 우선 keyPress
이벤트를 수신하지 마십시오! 사실 keyPress
이벤트는 반복적으로 실행되지 않을 수도 있습니다.
당신이해야 할 일은 keyDown
과 keyUp
이벤트를 경청하는 것입니다. 당신은 이것을 도우미로 만들 수 있습니다.
class Game {
Keyboard keyboard;
Game() {
keyboard = new Keyboard();
window.requestAnimationFrame(update);
}
update(e) {
if (keyboard.isPressed(KeyCode.A))
print('A is pressed!');
window.requestAnimationFrame(update);
}
}
:
class Keyboard {
HashMap<int, int> _keys = new HashMap<int, int>();
Keyboard() {
window.onKeyDown.listen((KeyboardEvent e) {
// If the key is not set yet, set it with a timestamp.
if (!_keys.containsKey(e.keyCode))
_keys[e.keyCode] = e.timeStamp;
});
window.onKeyUp.listen((KeyboardEvent e) {
_keys.remove(e.keyCode);
});
}
/**
* Check if the given key code is pressed. You should use the [KeyCode] class.
*/
isPressed(int keyCode) => _keys.containsKey(keyCode);
}
그럼 당신은 게임에 무엇에 따라, 당신은 아마 한 번에 한 동안 모든에서 호출되는 당신의 update()
방법에 어떤 종류의 "게임 루프"를,이 이제 게임 루프가 A
키를 반복적으로 확인합니다.
게임 루프가 끝나면 모든 키보드 누름을 지울 수 있습니다. 또한 https://github.com/sethladd/bad-aliens-dart를 참조하십시오. 업데이트해야 할 필요가 있음을 깨달았습니다. –
다트 게임에서 키보드 처리에 관한 기사를 작성했습니다 : http://dartgamedevs.org/blog/2012/12/11/keyboard-input/ – Cutch
@Cutch 링크가 작동하지 않습니다. 아마도 : https://github.com/dartgamedevs/dartgamedevs.org/blob/master/source/_posts/2012-12-11-keyboard-input.markdown – Amsakanna