Midnight Flash Game Design #3: Calculating Distance And Handling Keyboard Input

As I continue this project of creating a game in 27 days (hours), I have realized that my “one hour a night” estimate is almost perfectly on target. The first night of programming all about trying to fix the obvious problems with the original “Fireworks Blast” demo.

My first task then, was to make sure that all shells, no matter how high they are being fired, travel at the same velocity. For test purposes, I have set the velocity to 10 px per game loop cycle. I created a “Projectile” class, and had it calculate some important values when after being instantiated. The first thing each Projectile needs to know is where it is going. I use the variables finalX and finalY for this purpose. Secondly, it needs to calculate how far it is going to move on each movement cycle, in both the x and y directions. This is where I had the original bug. I solved this my using the the following Mathematical formula:

Math.sqrt(xd*xd + yd*yd)

In this formula, xd = the distance between the the first x value and the last x value, while yd = the distance between the first y value and the last y value. The full set of code to setup these values for calculating movement of the Projectile is here:

var xd:Number = finalX – this._x
var yd:Number = finalY – this._y
var Distance:Number = Math.sqrt(xd*xd + yd*yd)
fuse = Math.floor(Math.abs(Distance/Velocity));

xunits = (finalX – this._x)/fuse;
yunits = (finalY – this._y)/fuse;

xunits and yunits are the values that I will update this projectile on each movement cycle. They have been pre-calculated here for efficiency. Fuse is how many cycles this Projectile needs to move at Velocity to reach finalX and finalY . This original bug I had was keeping this value fixed, no matter the distance the shell was being fired.


The second problem I had to fix was the keyboard input. Since the players will be firing red, white and blue shells , I needed to create an easy to remember keyboard scheme. I chose the [Z], [X] and [C] keys to launch the basic fireworks, and then added the [SPACEBAR] to launch the “super fireworks”.

The first thing I needed to do was to let my game class (named “Show”) listen for the keyboard.

public function Show(par:MovieClip) {    


Next, I needed to add support for listening to the proper keys. I also needed to make sure players could not hold-down the key to launch multiple fireworks on purpose or by accident. I did this through a variable named keywait. Keywait is a simple counter that constantly counts up. If it is over 5, then the player can launch a firework shell. This would is 5 frames right now, but will most likely be changed when the game is optimized.

Here is my function for capturing the key presses and firing off the proper color shell;

function onKeyDown() {

if (keywait > 5) {

var keyval:Number = Key.getCode();
trace(“Key code:” + Key.getCode());
switch (keyval) {

case 90:


if (score1.getRedshells() > 0 ) {

fireMouse(_root._xmouse,_root._ymouse,Stage.width/2, Stage.height,0xFF0000);



case 88 :


if (score1.getWhiteshells() > 0 ) {

fireMouse(_root._xmouse,_root._ymouse,Stage.width/2, Stage.height,0xFFFFFF);


case 67 :

if (score1.getBlueshells() > 0 ) {

fireMouse(_root._xmouse,_root._ymouse,Stage.width/2, Stage.height,0x0000FF);


case 32 :

if (score1.getFlairshells() > 0 ) {

fireMouse(_root._xmouse,_root._ymouse,Stage.width/2, Stage.height,0xFF6600);




Here is my function for moving the projectiles and updating keywait so another one can be fired. This function is called every cycle as part of the main game loop.


function fMODE_PLAY_GAME () {



I’ve created a demo of this version. I’ve already started on the next version. This one will include the music and backgrounds necessary to create the 5 different levels for the game.




Leave a Reply