moosefetcher
moosefetcher

Reputation: 1901

AS3 Key held down preventing mouse update

I'm developing a 3D game and I've noticed some odd behaviour when I hold down the movement keys; the game seems to temporarily ignore the mouse position and stops updating which way the player is facing. The game does not freeze - it's just as if it stops paying attention to the position of the mouse.

Here's the code I'm using to get key presses and releases:

public function keyPressed(e:KeyboardEvent):void {
    switch (e.keyCode) {
                case 32:
                        _keySpace = true;
                        break;
                case 81:
                        if (!keyQ) {
                            keyQ=true;
                        }
                        break;
                case 69:
                        if (!keyE) {
                            keyE=true;
                        }
                        break;
                case 65:
                        if (!keyA) {
                            keyA=true;
                        }
                        break;
                case 68:
                        if (!keyD) {
                            keyD=true;
                        }
                        break;

                case 87:
                        if (!keyW) {
                            keyW=true;
                        }
                        break;
                case 83:
                        if (!keyS) {
                            keyS=true;
                        }
                        break;
    }
}


public function keyLift(e:KeyboardEvent):void {
            if (e.keyCode==32) {
                _keySpace = false;
            } else if (e.keyCode==81) {
                keyQ=false;
            } else if (e.keyCode==69) {
                keyE=false;
            } else if (e.keyCode==65) {
                keyA=false;
            } else if (e.keyCode==68) {
                keyD=false;
            } else if (e.keyCode==87) {
                keyW=false;
            } else if (e.keyCode==83) {
                keyS=false;
            }
        }

And the code to update the mouse position is just an ENTER_FRAME event:

addEventListener(Event.ENTER_FRAME, enterFrame);

...that triggers the main game loop. Here's the relevant part that uses the mouse position. The mouseX and mouseY properties are used in Player to rotate the view left/right and up/down.

private function enterFrame(e:Event=null):void {
    _player.update(mouseX, mouseY);
}

All of that works fine - it's just this problem with holding down the strafing keys; it's as if, once the key starts repeating, the game is taking all its time resolving those events and neglects to update the rotation - even though it's not dependent on a MouseEvent.

Does anyone know how to make Flash ignore repeating keys when the key is held down? Or is there some other problem?
Cheers.

Upvotes: 0

Views: 294

Answers (1)

micapam
micapam

Reputation: 763

You could check when your key handler was last triggered, and if it's more recently than a certain threshold, ignore it. You could make it a tenth of a second:

private static const KEY_THRESHOLD:int = 100; // 100 ms = 1/10 second

private static var lastPressedAt:int;

public function keyPressed(e:KeyboardEvent):void {
    var now:int = new Date().getTime();

    if (lastPressedAt > 0 && now - lastPressedAt < KEY_THRESHOLD) {
        return;
    }

    lastPressedAt = now;

    // etc...

Upvotes: 1

Related Questions