Part 4 : Moving The Player

Now that we have set described the basic set-up of the .fla, that basic state machine for the game, and basic event model we will be using, it is time to start making the actual game. In the last section we placed the Player object on the screen, and now we will look at the code for the class in player.as.

import mx.events.EventDispatcher;
class Player extends MovieClip {
 var speed:Number;
 var nextx:Number;
 var lbound:Number;
 var rbound:Number;
 var fireKeyWait:Number = 0;
 static var FIRE_WAIT_FRAMES  =  7;

 public function Player () {
  EventDispatcher.initialize(this);
  speed = 5;
  fireKeyWait = 0;
  lbound=0;
  rbound = Stage.width-this._width;
  nextx = _x;
  }

 function setLocation(x,y) {
  _y = y;
  _x = x;
  nextx = _x;
 } 

 function setSpeed(val:Number) {
  speed = val;
 }
 function getSpeed():Number {
  return speed;
 }

 function setBounds(right:Number, left:Number) {
  rbound=right;
  lbound=left;
 } 

 function Move() {
  if (Key.isDown(Key.LEFT)) {
    if (_x-speed > lbound) {
    nextx -= speed;
    }
  }

  if (Key.isDown(Key.RIGHT)) {
   if (_x+speed < rbound) {
    nextx += speed;
   }
  }
  fireKeyWait++;
  if (Key.isDown(Key.SPACE) && fireKeyWait > FIRE_WAIT_FRAMES) {
   this.dispatchEvent({type:"EventFireMissile"});
   fireKeyWait=0;
  }
 }

 function Render() {
  _x = nextx;
 }

 public function addEventListener(){/*Interface Stub*/}
 public function removeEventListener(){/*Interface Stub*/}
 public function dispatchEvent(){/*Interface Stub*/}
}

First, notice that we have included all the EventDispatcher code that we used in TitleScreen.as and HomeWars.as. Every class we create will have these function included, so I will cease describing it at this point.

The Player class does two basic things necessary for our game:

  1. Moves the player on the screen by listening for keyboard input (arrow keys)
  2. Send out a message that a missile has been fired by listening keyboard input (space bar)

Everything else in this class supports those two basic functions.

setLocation(): This function sets the initial location of the player on the screen. We also set nextx to _x in this functions. The Player class calculated nextx in the move() function and applies nextx to _x in render function. However, when the class is instantiated, and when we use setLocation(), we needed to make sure to update nextx so the next time render() is called it can be applied.

setBounds(): This function sets the right and left boundary (rbound, lbound) on the screen that our player can move to. We set the lbound to 0. This is because the registration point of the FPlayer MovieClip is in the top, center (-30,0), and if the player moves to a position of _y=0 on the screen, the middle side of the player graphic will just reach the edge of the screen. Since the player fires from the middle, we do this so they can shoot enemies at the far left of the screen. We set the rbound to Stage.width; This will set the rbound to the extreme right of the screen, but since the registration point is -30,0, it won’t reach this point until the middle Player hits that boundary.

setSpeed(): This function sets the speed at which the player will move.

Again, quickly, let’s look at the move() function:

function Move() {
      if (Key.isDown(Key.LEFT)) {
        if (_x-speed > lbound) {
        nextx -= speed;
        }
      }

      if (Key.isDown(Key.RIGHT)) {
       if (_x+speed < rbound) {
        nextx += speed;
       }
      }
      fireKeyWait++;
      if (Key.isDown(Key.SPACE) && fireKeyWait > FIRE_WAIT_FRAMES) {
       this.dispatchEvent({type:"EventFireMissile"});
       fireKeyWait=0;
       }

     }

This function works because it is called every frame by the move() event called from the HomeWars class. The Key.isDown() function is static function call to the always available in Flash Key object. It allows you to test is a key is currently pressed. We use this instead of the onKeyDown() key listener because, unlike onKeyDown(), it can be checked on demand, instead of waiting for a keyboard event. This might not sound like much, but for action games it is a must. onKeyDown() will only register a single key-press at a time. This will make for stutter-stop movement that is unacceptable. (However, it is perfect for creating a [pause] function as we will see later).

For moving left and right, all we have to do is check for left-arrow or right-arrow key press (Key.isDown(Key.LEFT) and Key.isDown(Key.RIGHT), and set nextx according to the current speed. However, testing the [space bar] for firing missiles is a bit harder. If we test every frame for a missile firing, the Player will simply be too powerful and fire far too many missiles to make the game any kind of challenge. Because of this, we will slow down the firing by creating a “waiting period” in which the player cannot fire a missile. This is certainly not the only way to solve this problem, but it has worked well for me in the past. Here is the code that tests for a missile being fired:

fireKeyWait++;
      if (Key.isDown(Key.SPACE) && fireKeyWait > FIRE_WAIT_FRAMES) {
       this.dispatchEvent({type:"EventFireMissile"});
       fireKeyWait=0;

The fireKeyWait property is used to count a specified number of frames (specified with the FIRE_WAIT_FRAMES property) before a missile can be fired. When a missile is fired, we set that value back to 0 and then count again. We will use this property to make the player more powerful in a later lesson.

Finally, this line of code: this.dispatchEvent({type:"EventFireMissile"}); will send a message back to all subscribers that the Player has fired a missile, and we need to do something about it.

If you ran the .fla file right now, with all the code intact, you should see something like the image below. The arrow keys would move the Atari 800 right and left.

4 moving the player atari 800

Read the rest of the series: ‘Anatomy of a Flash Game’

  1. Anatomy of a Flash Game: Lesson 1 – Setting up the game
  2. Anatomy of a Flash Game: Lesson 2 – Creating Enemies & The Game Environment
  3. Anatomy of a Flash Game: Lesson 3 – MochiAds, MochiBot and MochiAds Leaderboards