PDA

View Full Version : [AS3] Strange multiple key behaviour


Gijsj
05-09-2007, 07:06 PM
I'm just experimenting with AS3 and what is more fun than building a little game, so I started with the writing of a Player class. To move my Player I use an array to check for multiple key presses, including the spacebar, which is used to fire. This works fine when I press left+up+fire, down+right+fire, up+down+fire, left+right+fire, but not when I press right+up+fire or left+down+fire.

The code below shows three event handlers of which two keyboard events and an enter frame event.


private function keyDownHandler(event:KeyboardEvent):void {
if ((event.keyCode == 37) && (_direction.indexOf("left") == -1)) _direction.push("left");
if ((event.keyCode == 38) && (_direction.indexOf("up") == -1)) _direction.push("up");
if ((event.keyCode == 39) && (_direction.indexOf("right") == -1)) _direction.push("right");
if ((event.keyCode == 40) && (_direction.indexOf("down") == -1)) _direction.push("down");
if ((event.keyCode == 32) && (_direction.indexOf("fire") == -1)) _direction.push("fire");
}

private function keyUpHandler(event:KeyboardEvent):void {
var index:int = -1;
if (event.keyCode == 37) index = _direction.indexOf("left");
if (event.keyCode == 38) index = _direction.indexOf("up");
if (event.keyCode == 39) index = _direction.indexOf("right");
if (event.keyCode == 40) index = _direction.indexOf("down");
if (event.keyCode == 32) index = _direction.indexOf("fire");
if (index > -1) _direction.splice(index, 1);
}

private function run(evt:Event):void {
if (_direction.indexOf("left") != -1) x -= _speed;
if (_direction.indexOf("right") != -1) x += _speed;
if (_direction.indexOf("up") != -1) y -= _speed;
if (_direction.indexOf("down") != -1) y += _speed;
if (_direction.indexOf("fire") != -1) this.fire();
}


Any ideas?