The Spawn Engine

an educational open source Flash game engine

How to add more class files

With this tutorial I’m going to show how to add more class files to the Spawn Engine. I’m using version 0.1.2 and CS5. We will add this to later versions of TSE but for now it’ll just be a tutorial showing you how to add it yourself and explaining why this is good practice.

Action Script 3 is an object oriented language (OOP). I’m not going into detail on what OOP is since there are plenty of articles out there doing a better job than I’d do. Check out these two!

First the steps, then we’ll discuss what we’ve won.

1) Right click in your library on the enemy movie clip and choose Properties. There is already a Class linked to this movie clip: Enemy. But that class don’t have any file, so we are going to make one now.

2) Click on the pen button “Edit class definition” and CS5 opens a new tab with an as-file for the class Enemy. The class is empty except for the basic structure (package, import, class and constructor). So we are going to move a few things from the engine to this class.

3) Save the file as Enemy.as in the same folder as your other files for this project.

4) Inside the Enemy class add the line

private var speed:int;

private const MIN_SPEED:int = 7;
private const MAX_DIFF_SPEED:int = 6;

This will be the move speed of the enemy movie clip which earlier was found in the engine. We need to remove it from the engine and only have this data inside the Enemy class.

5) Inside the public function Enemy (the constructor) we add the line

speed = MIN_SPEED + Math.random() * MAX_DIFF_SPEED;

We want each new enemy that is spawned to have its own unique speed. In version 0.1.2 all enemies have the same speed 9 set by a constant. What will the speed of these new enemies be?

6) Under the constructor we add a new function getSpeed that should look like

public function getSpeed():int
{
return speed;
}

Why did we do this?

7) There are a few changes that we need to do in SpawnEngine.as now. Find this lines of code and comment it so it looks like this

//private const ENEMY_SPEED:int = 9;

We no longer need this constant since that data is now stored inside each Enemy object.

8) Find this line

enemies[i].y += ENEMY_SPEED;

and rewrite it to

enemies[i].y += enemies[i].getSpeed();

We want to use the new function that returns the variable speed which now is a unique value for each Enemy object.

8) Test your changes! You should be able to see some enemies moving faster than others. If you want to change the minimum and maximum speed of the enemies you’ll need to go the the Enemy class file and change the constants we added in step 4.

What your Enemy.as should look like now

package {
import flash.display.MovieClip;
public class Enemy extends MovieClip
{
private var speed:int;
private const MIN_SPEED:int = 7;
private const MAX_DIFF_SPEED:int = 6;
public function Enemy()
{
// constructor code
speed = MIN_SPEED + Math.random()*MAX_DIFF_SPEED;
}
public function getSpeed():int
{
return speed;
}
}
}

Why did we do this?

This was just one small step in the right direction. We moved one piece of data, speed, from the engine into a separate class file. The engine don’t need to know everything and do everything. With more class files the engine will be shorter and more manageable. We put the functions and the variables where they belong, inside the class of the object. This also allows us to do things that were difficult to do before, like adding a unique speed to each enemy. Or maybe we want to add health to each enemy or some other value inside each enemy. This is much easier to do when the enemy has its own class. To improve the enemy class we could move more functionality to it from the engine. Each enemy can move itself instead of the engine doing the work for example.

Could we have done this differently?

Now we wrote the Enemy class to fit into the engine. I wanted to show this while making very small changes to the engine. We wrote a function getSpeed and we could of course have done that differently. We could have written a function inside the Enemy class where the enemy moves itself instead of a function that sends the speed to the engine so the engine can move the enemy. Had we written this class file at the same time we wrote the engine things would be different from now when we write the class file after the engine. Future versions of the engine will show this.

What’s next?

Now that we have done this small change on enemies you can do this for the pickups and more.

2012/03/07 Posted by | Tutorials | , , , | Leave a comment