Hello guys,
I have a very strange problem and i can’t get it fixed. I’m trying to let my ship move with WASD. I’ve got it to work with the arrow keys, no problem.
It seems to be the problem that my event isn’t triggered when i press W, A, S or D. Because, when i place a trace(event.keyCode), it doesn’t trace anything.
I’m using this within a class.
Here’s my (part of the) code:
public function ship() {
stage.addEventListener(KeyboardEvent.KEY_DOWN, keysDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keysUp);
addEventListener(Event.ENTER_FRAME, onEnter);
}
and:
private function keysDown(event:KeyboardEvent):void {
switch (event.keyCode) {
case 87 :
upPressed=true;
break;
case Keyboard.DOWN :
downPressed=true;
break;
case Keyboard.LEFT :
leftPressed=true;
break;
case Keyboard.RIGHT :
rightPressed=true;
break;
case Keyboard.SPACE :
spacePressed=true;
break;
}
}
private function keysUp(event:KeyboardEvent):void {
switch (event.keyCode) {
case 87 :
upPressed=false;
break;
case Keyboard.DOWN :
downPressed=false;
break;
case Keyboard.LEFT :
leftPressed=false;
break;
case Keyboard.RIGHT :
rightPressed=false;
break;
case Keyboard.SPACE :
spacePressed=false;
break;
}
}
In the code above, the W key doesn’t work. But the left, right and down arrow keys work fine. If i change those to the corresponding key values of a, s and d.
It doesn’t work anymore.
Please help me,
Please note: I’m new to as3. I’ve been programming with as2 for 2 years. Now i’m migrating to as3.