[RESOLVED] [HELP][AS2] Detecting Keys
Alrighty.. So I'm working on this very small project, but I've run into some trouble surprisingly very early in. :crazy:
The game is a basic platformer. To detect the key strokes I'm using the good'ol (or so I thought) Key.addListener method.
MY PROBLEM: if I hold the right key down and then tap the up key, the listener forgets about the right key still being held down, (and upon the release of the up key while the right key IS STILL DOWN) the onKeyUp function activates. Because of this, the listener ignores the right key which IS STILL being held down, and thinks that since it recorded the up key being released that no other keys are being held down.
I realise that this is the nature of using a listener, but for reasons I'm not going to go into detail about I'm not going to ditch this method and use Key.isDown instead. So yeah, it'd sweet if someone could suggest a fix for this, even if it is a lil’ hacky.
Here's the relevant code:
PHP Code:
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
keyDown = getKey();
moveChar(keyDown);
};
keyListener.onKeyUp = function() {
if(getKey()!="UP") {
keyDown = " ";
}
};
//add the mouse listener
Key.addListener(keyListener);
function getKey ():String {
var theKey:String;
switch (Key.getCode()) {
case Key.LEFT :
theKey = "LEFT";
break;
case Key.RIGHT :
theKey = "RIGHT";
break;
case Key.UP :
theKey = "UP";
jumpChar();
break;
case Key.DOWN :
theKey = "DOWN";
break;
default :
theKey = chr(Key.getAscii());
}
trace(theKey);
return theKey;
};
Thanks in advance for any helpful suggestions,
Viza.