I have 2 eventListeners:
1) key listener(A, S, D ,W); – for move object
2) key listener(WhiteSpace) – for jump object
so when i have pressed the A OR S OR D OR W keys then i press the whitespace to before this moment its all okey, the object is moving and jumping at the same time, but if i release the whitespace key while moving, the object stops…
so how can i make that when i release the key white space, the object will still moving?? don’t paying attention to the other key releasing or pressing?
private function onKeyDown(e:KeyboardEvent):void {
//trace(e.keyCode);
switch(e.keyCode)
{
case 68:
direction = 'left';
stage.addEventListener(Event.ENTER_FRAME, moveRight);
break;
case 65:
direction = 'right';
stage.addEventListener(Event.ENTER_FRAME, moveLeft);
break;
case 32:
jump() // the whitespace key
break;
}
private function moveRight(e:Event):void {
shape.x += 5;
}
private function moveLeft(e:Event):void {
shape.x += 5;
}
private function jump():void {
stage.addEventListener(Event.ENTER_FRAME, jumpAnimation);
}
private function jumpAnimation(e:Event):void {
//here code for jumping increasing the y and decreasing....
}
private function onKeyUp(e:Event):void {
stage.removeEventListener(Event.ENTER_FRAME, moveRight);
stage.removeEventListener(Event.ENTER_FRAME, moveLeft);
}
You could implement a key up/down listeners and an enter frame listener and use some booleans to indicate what keys are down and update accordingly. Something like this might work:
This would avoid a situation where one key up is causing all other keys from firing continuously because you would only be relying on a single key up or down from each key. The code to update your scene would be running continuously.