/ Published in: ActionScript 3
Listen to the Navigation keys and save their values in local Booleans
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
private var goRight:Boolean = false; private var goLeft:Boolean = false; private var goDown:Boolean = false; private var goUp:Boolean = false; public function Main() { stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler); stage.addEventListener(KeyboardEvent.KEY_UP, upHandler); } private function keyHandler(e:KeyboardEvent):void { switch (e.keyCode) { case Keyboard.UP: goUp = true; break; case Keyboard.DOWN: goDown = true; break; case Keyboard.LEFT: goLeft = true; break; case Keyboard.RIGHT: goRight = true; break; default: trace(e.keyCode); break; } } private function upHandler(e:KeyboardEvent):void { switch (e.keyCode) { case Keyboard.UP: goUp = false; break; case Keyboard.DOWN: goDown = false; break; case Keyboard.LEFT: goLeft = false; break; case Keyboard.RIGHT: goRight = false; break; default: trace(e.keyCode); break; }