Reputation: 123
I'm trying to get a standard keyboard-controlled game using AIR to accept my key presses. This game requires the ability to be able to run in one direction, and quickly turn around to run in the other direction; however, when I quickly switch from keys "A" to "D" in succession, my character stops in place for a moment and then continues as if I was moving my character in the ON_KEY_DOWN event. I understand how to use ENTER_FRAME to move the character using Boolean values, and I've viewed tutorials by other people (which I noticed had this exact problem in their tutorial as well).
This is a snippet from the Player.as class:
private function OnEnterFrame( e:Event = null ):void
{
if ( Input.kd("W") )
{
this.y -= runSpeed;
SetAnimation( RUNUP );
}
else if ( Input.kd("S") )
{
this.y += runSpeed;
SetAnimation( RUNDOWN );
}
else if ( Input.kd("A" ) )
{
this.x -= runSpeed;
SetAnimation( RUNLEFT );
}
else if ( Input.kd("D") )
{
this.x += runSpeed;
SetAnimation( RUNRIGHT );
}
else
SetAnimation( IDLE );
}
This is a snippet from the Input class (which was borrowed from Matthew Bush in an attempt to study this strange issue)
public static function initialize(s:Stage, autoClear:Boolean = true):void {
if(initialized) {
return;
}
initialized = true;
if(autoClear) {
s.addEventListener(Event.ENTER_FRAME, handleEnterFrame, false, -1000, true); /// Very low priority.
}
s.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown, false, 0, true);
s.addEventListener(KeyboardEvent.KEY_UP, handleKeyUp, false, 0, true);
s.addEventListener(MouseEvent.MOUSE_UP, handleMouseUp, false, 0, true);
s.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown, false, 0, true);
s.addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove, false, 0, true);
s.addEventListener(Event.MOUSE_LEAVE, handleMouseLeave, false, 0, true);
s.addEventListener(Event.DEACTIVATE, handleDeactivate, false, 0, true);
stage = s;
}
/**
* Record a key down, and count it as a key press if the key isn't down already.
*/
public static function handleKeyDown(e:KeyboardEvent):void {
if(!keysDown[e.keyCode]) {
keysPressed[e.keyCode] = true;
keysDown[e.keyCode] = true;
}
}
/**
* Record a key up.
*/
public static function handleKeyUp(e:KeyboardEvent):void {
keysUp[e.keyCode] = true;
delete keysDown[e.keyCode];
}
And here is the output:
Key Down: 68 //Holding D
Key Down: 65 //Let go of D and pressed A
Key Up: 68 //D KeyUp fires
Key Up: 65 //A KeyUp fires, but I only just pressed A and didn't let it go
Now, a scenario for further clarification: Say I'm holding D to make the character run to the right, all is fine at this point; I quickly let go of D, and press A to turn in the other direction, that's where the issue happens. While tracing on handleKeyUp, it says that both D and A were let go at the same time even though I just pressed A, it was never released. This removes both D and A from my dictionary of held keys, causes the player to stop for a split moment, and then as soon as the handleKeyDown kicks in, he begins to run in the correct direction. What can I do about this? Please let me know if you need further clarification or more code.
Upvotes: 2
Views: 387
Reputation: 123
I've found what my actual problem is here. Upon going home from work, my game worked perfectly fine with my current code, and coming back to work today, my game was not working fine. It turned out the issue was with my keyboard specifically; I'm using an older keyboard with a Ps/2 cable connected to a USB converter. When I replaced the keyboard with a USB keyboard, there was no issue. I still plan on finding a way around this issue for my Ps/2 cable keyboard because I don't want to ignore that sort of issue.
Upvotes: 1