PDA

View Full Version : [AS3] Problems with specific movement


kaptain kosher
06-30-2009, 08:39 PM
I'm making a simple game to orient myself with AS3, and right now I have an arrow key controlled character that move around. Specifically I want to character to move when I push down an arrow key, then if I keep the original arrow key down and push down another direction the character will go in the new direction and if I let the new key go the character will resume moving in the original direction. No diagonal movement is allowed.

So far I have gotten this to work if I am holding down a vertical key (up, down) and push and let go of a horizontal key (left, right). However, if I'm holding down a horizontal key and tap a vertical key the character won't move in the new direction.

At the moment I'm using four booleans (tryingToMoveLeft, tryingToMoveRight, etc.), and event listeners. When I push down an arrow key the corresponding boolean is true, and when I let it go it becomes false. Then every frame, if a boolean is true then the character moves in that direction.

By changing around my if-else statements I have found that I can change the movement but I've tried what seems to be every combination and haven't gotten the desired results.

bluemagica
06-30-2009, 10:11 PM
what you are trying to do is actually a bit hard, cause of if...else, there will always be a priority in the keys.....what you can do is maintain an array to see the last key pressed....

whenever a key is pressed, push it in the array, if it's not already in there, and on release, splice it out! And on your enterframe handler, just move based on the last value in the array!

kaptain kosher
06-30-2009, 10:29 PM
Ah yes... thank you very much. That'll work very nicely.

P.S. my bad on posting in the wrong topic :/