AS2 had a nice but odd function called Key.isDown(n), which would return true if the key with keyCode n was down. This worked very nicely, but didn’t fit with AS3’s new event model. Now Key.isDown() is gone, and its sleek functionality has gone with it.
However, I have a recommendation for designing your code when it determines which keys are down. By implementing this design, code that previously relied on Key.isDown() can be transitioned pretty easily to AS3, and IMO this will keep your AS3 a little neater.
Somewhere in your code, put together the following:
var keysDown:Array = [];
stage.focus = stage;
stage.addEventListener(KeyboardEvent.KEY_DOWN);
stage.addEventListener(KeyboardEvent.KEY_UP);
function addKey(event:KeyboardEvent):void {
keysDown[event.keyCode] = true;
}
function removeKey(event:KeyboardEvent):void {
keysDown[event.keyCode] = false;
}
// here's how you'd implement it:
var timer:Timer = new Timer(25);
timer.addEventListener(TimerEvent.TIMER, tick);
function tick(event:Event):void {
// old way- if (Key.isDown(Key.RIGHT)) {
if (keysDown[Keyboard.RIGHT]) {
trace("right key is down!");
}
// etc.
}
By storing the states of all the keyboard’s keys in the keysDown array, any key’s current up/down state can be determined by any code that can access keysDown.
Note that, due to a bug in Flash Player 8.5 and below, this code can be run in AS2, but doesn’t run well. For AS2, I would recommend always using Key.isDown(), and for AS3, use a system like the one above.