FLEX / PHP / mySQL / HTML / JAVA
Methods and Madness
During the development of Dash-8 Defender, there were a wide variety of
software obstacles to overcome. The following are the main
problems that arose and what I chose for solutions. I
don't claim that they are the best or most elegant solutions,
only that they worked. I'm putting them up here to
hopefully save some of you a bit of work, and also to save me
figuring out what I did a year or two from now! If you have any suggestions on how
to do it better or more cleanly, please email me and I will post your method.
Cheers.
1. Keyboard Input for Game Controls
It is very easy to use the mouse in flex to provide input to
any object. Keyboard control is a different story.
Initially, I thought to simply use the keyUp and keyDown events
to check for when a key was pressed (and released), then use the
event.charCode to determine which one. But the active
part of Dash-8 Defender sits on a Canvas, and a Canvas
can't take focus. If a canvas can't take focus,
then the keyUp and keyDown events never get sent
despite the user hammering on the keys. This one took me
two days until I discovered...
Solution:
Make a small invisible TextInput container and put it in the
Canvas. Then, when game play begins make sure you
set the focus to the TextInput container with the
setFocus parameter.
XML Code
<mx:Canvas x="0" y="0" width="800" height="600" id="myCanvas"verticalScrollPolicy ="off">
<mx:TextInput width="0" height="0" x="0" y="0" id="controlBox" visible="false"/>
</mx:Canvas>
Action Script Code where gameplay begins:
controlBox.setFocus();
As a follow-up, you should repeat the call to set the
focus into the TextInput container at the beginning of
each frame in case the user clicks the mouse outside of the
application. When the user clicks back, the focus
will immediately return to the TextInput container thus
allowing keyboard game commands to continue.
PS. I
read somewhere much later that if you want to give focus
to a Canvas you could just define another class
which would extend Canvas with the additional property of
being able to receive focus. This might be a more
elegant way to solve this problem. As for now, if it ain't
broke...
1 2 3 |