A solution to multiple keys being held down at once. When you listen to KEY_UP in flex, or actionscript 3.0 there are some issues by acting on the KEY_UP Event. Especially for games in actionscript 3.0.

Application Source

In the Application, start off by pressing “D” to move right, and “A” to move left. The problem is that when you are holding D to move right, lets say you press “W” to move up (now i don’t have W set up to work in this application, but none the less) the whole point is to have “D” still executing while you lift up back off from “W”.

To accomplish this, I created a keyboardManager Singleton class, the class holds an array of keys that are down (downKeys:Array). and 2 functions that execute on keyUp and keyDown.

On keyDown it takes a look at the downKeys array and see’s if it is empty… if so, then it adds the keyEvent to the array, and then it exits…

On keyUp it loops over the downKeys Array and splices it removing the correct keyEvent out of the array….

There is one trick that I do though… there is an outer document that actually adds the listeners for the keyUp and keyDown, in those listener functions, thats where I call the keyboardManager. The trick is on the keyUp listener, I do some checking to find out if I need to re-call the keyDown functions for the keys that are still down…

Typically in the type of game that I am making, I’m only going to be using W,S,D,A for my keys then adding some mouse interaction later on. I wanted the feel with the keyboard to be better than some of the examples I’ve seen, where the KeyUp Event is where things just get removed or speed gets set back to zero…

I wanted to be able to press “D”, then press “A” to back up a little, and take my finger off of “A” with the original “D” still pressed, and have “D” still be getting called…

Anyway it’s late, and I’m tired (it’s 2:45 am)…

Night…

ps… 360flex kicked ass… Thanks to the crew of guitar hero’ers that made it feel real homey… and for the huge attempts at getting rockband working…. a big thanks to Tom, and John for putting on the conference… it was by far the best conference i’ve been to.

Application Source