Over the course of several lessons, I will be describing in detail how to create a very basic game in Flash. We will be building a very simple shooting game named “Home Computer Wars”, inspired by all my friends who wanted make games in the 80’s, but never succeeded. In this game you will play “Atari” in an attempt to fight off the onslaught from IBM, Apple, Commodore and Texas Instruments computers.

We will be coding this game in ActionScript 2. However, we will be using an object oriented messaging system that can, with some effort, be ported to ActionScript 3.

Tutorial Outline:

  • Lesson 1: Game Setup – We first learn how to set up our game framework in Flash, and then create a player that can move and fire missiles.
  • Lesson 2: Creating Enemies & Game Environment – We add a scoreboard and waves of enemies that gradually get harder and harder to fight.
  • Lesson 3: MochiAds, MochiBot, and the MochiAds Leaderboards – We add MochiAds, MochiBot and the MochiAds Leaderboards service
  • Lesson 4: Finishing Touches – Balancing And finishing: Level Balancing, Sound fx, Logo Screen

Read on for Lesson 2!

Part 1: ScoreBoard

To continue making our game we will need start tracking data about how the player is progressing, and about how the game progresses.

To do this, we will create a ScoreBoard object that will serve as our player and game tracker for the game. Scoreboard will double as the MovieClip that displays the scoreboard information, so this object is very important. First, let’s see at what the FScoreBoard in the library looks like:

scoreboard

    Score: The player’s score
  • Year: This is how we will track “levels”. Each new “Year” will bring harder and harder enemies for our trusty Atari 800 to fight. We start at 1979 and go through the 80’s.
  • RAM: The amount of RAM will track how much fire-power the player has. The amounts are 8K, 16K, 32K and 48K.
  • Lives: The player starts with 3. We will display up to 5. After 5, we will place a number next to lives to display how many the player has left.

Note To Die-Hard Atari Fans: I know that there was never an Atari 800 with 8K memory. I thought it would make the game more enjoyable if you could have more upgrades. If this still makes you upset, here is a little story for you. Back in the 60’s when Elvis was making a movie, he was in a scene where he was sitting in the back of a car, pretending to play guitar and and lip-synching to a fully accompanied version of one of his big hits. Elvis stopped the the filming, upset and wanted to talk to the director. He told the director that the scene was totally unrealistic. There was no way he would be sitting in the back of car, playing guitar and singing a song, it would be physically impossible. The director, without missing a beat, replied “Elvis, where’s the band?” Elvis quit complaining and finished the scene. You see, sometimes fun trumps realism. Likewise, there might not have been an Atari 800 with 8K, but there was also not one that could shoot red plasma balls at invading IBM PC XTs either, so let’s just drop it.

The code for the ScoreBoard.as class looks like this:

import mx.events.EventDispatcher;
class ScoreBoard extends MovieClip {

	var score:Number;
	var year:Number;
	var cpus:Number;
	var k:Number;
	var ram:String;
	var cpuDisplay:MovieClip;

	var years:Array;

	function ScoreBoard() {
		EventDispatcher.initialize(this);

	}	

	function reset() {
		setScore(0);
		setYear(1979);
		setCPUs(3);
		setK(8);
		years = new Array();

	}
	function setScore(val:Number) {
		score = val;
	}
	function getScore():Number {
		return score;
	}
	function setYear(val:Number) {
		year = val;
	}
	function getYear():Number {
		return year;
	}
	function setCPUs(val:Number) {
		cpus = val;
		cpuDisplay.gotoAndStop(cpus);
	}
	function getCPUs():Number {
		return cpus;
	}
	function setK(val:Number) {
		k = val;
		ram = k.toString()+"K";
	}
	function getK():Number {
		return k;
	}

	public function setLocation(x:Number,y:Number) {
		this._x = x;
		this._y = y;
	}

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

This class is very straightforward. Most of the functions are very basic getter and setter functions for the various information we will be tracking about the game.

K = RAM = firepower for the computer.

Year = Level;

The only slightly tricky function is the setCPUs. “Lives” in Home Wars are counted in CPUs. The player will start with 3 CPUs, and gain them by achieving specific point levels. We will have a graphical display of the remaining CPUs the player has in the game. The FScoreBoard movieclip in the library contains another movieclip named CPU display that looks like this:

cpu-display

For this game we will show up to five lives on the screen. If the player collects more lives than 5, a number will be displayed to the right of of the CPU display. This number displays the current value of the CPUs property of the ScoreBoard class. After creating the ScoreBoard class, and FScoreBoard MovieClip, we need to display it on the screen. We will do this in the HomeWars class. The first thing we need is to choose a depth for the ScoreBoard. We want the ScoreBoard to appear above everything else on the screen. We will set a static var in HomeWars for this value:

        static var SCOREBOARD_DEPTH		= 15000;

We also need a variable to hold our instance of ScoreBoard. We define it like this:

       var scoreboard:MovieClip;

Finally, we need to display the ScoreBoard on the screen when the game starts. We will do this in the fSTATE_INIT function. It will look like this:

         scoreboard = parentTL.attachMovie("FScoreBoard","sb",SCOREBOARD_DEPTH);
         scoreboard.setLocation(0,0);
         scoreboard.reset();		

Notice that we used the SCOREBOARD_DEPTH static variable to assign a depth to the instance of Scoreboard.

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