Part 3 : Initializing The Game
So now that we have the main loop taken care of, let’s start building an actual, functional game. For this lesson, we are going to do the following:
- Display a TitleScreen
- Initialize a new game
- Put the player on the screen
- Capture movement for the player and fire missiles
- Allow the game to be paused
First, we will add some code to set depths for the object on the screen. Setting depths has been the bane of existence for Flash game developers for many years. We will try to make it as easy as possible for this game. By the way, AS3 adds the addChild(), which makes the old way of managing depths unnecessary. However, this opens up its own new set of difficulties.
static var PLAYER_DEPTH = 1000; static var MISSILE_MIN_DEPTH = 2000; static var MISSILE_MAX_DEPTH = 3000; static var TITLE_SCREEN_DEPTH = 10000; static var END_SCREEN_DEPTH = 10010;
The variables above were created as “static” so that, if need be, we could discover their values from an outside class without having to create an instance of “HomeWars”. In reality, we will probably never need to get these static values from outside this class, but this is an example of what to do if that was a requirement for your game.
Next, we need to set-up the constructor for our class. The class constructor is always a function that is named the same thing as the class. It gets called automatically when a class is instantiated. Instantiating a class simply means creating an object or instance of that class type that you can use in your code. A class instance has a set of it’s own poperties (variables) and methods (functions) that operate independently of other objects.
Here is our constructor code:
function HomeWars(pt) {
EventDispatcher.initialize(this);
parentTL = pt;
gameState = STATE_TITLE_SCREEN ;
}
EventDispatcher.initialize(this);
This line of code sets up the class to use the Flash Event model for creating custom events. We will be using this model to communicate among the various objects in the game.
parentTL = pt;
We need a Flash timeline on which to attach all of our objects. We will be using the root timeline for this game, but this could be any MovieClip that is set to location 0,0 on the screen.
Notice that we set the gameState to “STATE_TITLE_SCREEN“. This will make our switch() statement in the run() function move the game into that “state” the next time it fired from onEnterFrame. Now we will look at the the functions for showing the title screen. This only important for our discussion because it shows how to handle very basic custom events in Flash. The three functions necessary to show the title screen are below:
function fSTATE_TITLE_SCREEN() {
titleScreen = parentTL.attachMovie("FTitleScreen","titlescreen", TITLE_SCREEN_DEPTH);
titleScreen.setLocation(0,0);
titleScreen.addEventListener("EventCloseTitleScreen", this);
gameState = STATE_WAIT_FOR_CLOSE;
}
This function attaches the FTitleScreen object from the library, displaying it at the depth specified in TITLE_SCREEN_DEPTH, and place it at 0,0 on the screen. It then adds the HomeWars object instance as a “listener” for the event “EventCloseTitleScreen”. Here is what the titlescreen will look like when it is shown:

(Note: See that weird looking type-writer looking thing at the bottom of the screen? That’s an Atari 800, the greatest 8-bit home computer ever made. Don’t ask for evidence, just believe)
Finally, it set’s the game state to go into “wait” mode. This is an empty function that will be called until the user closes the TitleScreen to play the game.
function EventCloseTitleScreen(e:Object) {
titleScreen.removeMovieClip();
gameState = STATE_INIT;
}
The function above is the event that will be called from the instance of TitleScreen when the “Play” button is pressed. Notice that the name of the function is the same as the name of the event. I like to prefix all my Event callback function names with “Event” just for self-documentation purposes, but they do not have to be named that. However, they do need to be named the same thing as the Event specified in the addEventListener function.
function fSTATE_WAIT_FOR_CLOSE() {
//waiting
}
As stated before, the function above does nothing. It’s just a wait state. We can reuse this function whenever our state is to simply wait for user interaction. Now we must create the TitleScreen.as file that will work with our FTitleScreen object in the library. By using attachMovie()with a MovieClip associated with a class in the library, you will create an instance of that MovieClip that has all the properties and functions of the class. By right-clicking on aMovieClip in the library you can set the associated class for a MovieClip:

Here is what the full code looks like:
import mx.events.EventDispatcher;
import mx.utils.Delegate;
class TitleScreen extends MovieClip {
var play_button:MovieClip;
function TitleScreen() {
EventDispatcher.initialize(this);
play_button.onRelease = Delegate.create(this,EventClickPlayButton);
}
public function setLocation(x:Number,y:Number) {
this._x = x;
this._y = y;
}
function EventClickPlayButton() {
this.dispatchEvent({type:"EventCloseTitleScreen"});
}
public function addEventListener(){/*Interface Stub*/}
public function removeEventListener(){/*Interface Stub*/}
public function dispatchEvent(){/*Interface Stub*/}
}
The most interesting parts of this class are described below.
play_button.onRelease = Delegate.create(this,EventClickPlayButton);
This line of code in the constructor sets the [PLAY] button to call EventClickPlayButton when the user clicks it. This is commonly known as a callback function. The Delegate.create() static function call is used to assign the context of the TitleScreen object to play_button when it is pressed. If we did not do this, the EventClickPlayButton() function would be called as if it was part of the play_button object, and it would not have access to our instance of TitleScreen without using _parent. This is a messy practice, and can sometimes get you into variable scope issues. By the way, this has been fixed in AS3, and you will not need to use Delegate() to access the context of the class that has assigned a callback function to an object.
function EventClickPlayButton() {
this.dispatchEvent({type:"EventCloseTitleScreen"});
}
This function dispatches the EventCloseTitleScreen event to all the objects listening for it. In this case, our instance of the HomeWars class is a listener. The parameter passed in the calls to dispatchEvent is an object, and it can contain multiple parameters. The Type parameter is mandatory, because it describes the event that is being dispatched. We will be using this basic event model throughout the construction of the game, and I will avoid describing it again. Just remember, one object must subscribe to an event using the addEventListener() function of said object, and any object that wants to broadcast that event to all subscribers must use dispatchEvent.
Recall that after this Event is broadcast, the function EventCloseTitleScreen() in our instance of HomeWars will be called.
function EventCloseTitleScreen(e:Object) {
titleScreen.removeMovieClip();
gameState = STATE_INIT;
}
This function deletes the TitleScreen object, and sets the gameState to initialize the game. The switch() statement in our run() function will now bet set to call this function.
function fSTATE_INIT() {
player = parentTL.attachMovie("FPlayer", "player1", PLAYER_DEPTH);
player.setLocation((Stage.width/2)-(player._width/2),Stage.height-player._height);
this.addEventListener("Move",player);
this.addEventListener("Render",player);
gameState = STATE_PLAY;
}
We will initialize the game by placing our player in the screen, (the first two line) and set-up the events necessary for it to interact with HomeWars (the second 2 lines). Placing the player on the screen is done in the same way we placed the TitleScreen object on the screen.
this.addEventListener("Move",player);
this.addEventListener("Render",player);
All of the on-screen objects will listen to these two events: Move and Render. Move will calculate the next position of the object and Render will actually place it in it’s new position. This structure is not really necessary for the game we will be making, but for anything more complex it would be essential. The theory behind it is this: You calculate the Next position of your objects, test collisions on those calculations, then Render all the objects to the screen. This is done to prevent objects from passing through each other if their speeds are such that they would jump too many pixels to register a “hit” if all were moved at once. While this game is too simple to deem this necessary, a “Breakout” style game, for example, would require it so that the ball would not pass through bricks or walls unexpectedly.
Read the rest of the series: ‘Anatomy of a Flash Game’
- Anatomy of a Flash Game: Lesson 1 – Setting up the game
- Anatomy of a Flash Game: Lesson 2 – Creating Enemies & The Game Environment
- Anatomy of a Flash Game: Lesson 3 – MochiAds, MochiBot and MochiAds Leaderboards

Wow, just what I’ve been looking for. Nice article.
Just curious–
In section 4, what does the ‘F’ prefix indicate in your Movie Clip names and identifiers?
It’s just a convention. Back when we were building everything with components, one of our developers told us “that was the way to do it”, and I’ve continued to do it. It helps to find the assets that have classes associated with them, but in rteality, any convention you use (or none) should work fine.
You files to download are different then the code in the tutorial. For some reason, my title screen won’t load. I’ve tried putting trace’s
to see where things are being triggered.
I put a trace in the function
function fSTATE_TITLE_SCREEN() to see if it was even getting in there but it is.
Then I decided to put a trace in the TitleScreen.as
Trace did not go through. I checked many times to see if maybe linkage Identifier was incorrect or class was incorrect and they were not. I also checked to see to make sure that I had unchecked export actionscript on frame 1.
So, not sure what the problem is. Any help would be appreciated or if you could post the files that you use the code from this tutorial.
Because I noticed the code posted for the file TitleScreen.as does not include the create Delegate.Create.
Also, the code on the main.fla is different as well.
Well, that won’t suffice. Can you .zip your files and email them to me so I can take a look at them?
Send thjem to: stevefulton@adelphia.net
Also, check this:
1. Put a travce in the constructor of TitleScreen.as to see if it ever gets called.
2. Make sure the class is named TitleScreen in the TitleScreen.as and the contructor is named function TitleScreen()
-Steve
Going to send you my files zipped.
Thank you for taking interest in my problem i appreciate it.
jeff
Nice piece! Learned a few things, disagreed with a few things :o) Have you benched the event dispatcher versus normal method calling?
fyi you can use: for (var i in array) {item=array[i]} to interate backwards through an array , plus it is faster…
I downloaded the files but I can’t get it working in Flash CS3. It firstly complained about missing classes ScoreBoard, Enemy, PowerUp, and EndScreen. It also complained about being unable to find the ComponentBase class. I fixed the first four errors by creating empty stub classes for those but I have no idea how to fix the ComponentBase issue….
I do like the tutorial and I’m looking forward to the remaining installments as even though I’m an experienced programmer I’m finding it quite a challenge to develop an interactive application in Flash as I’m not sure how to tie the MovieClips and the code together.
It sounds likw the wrong set of files was zipped and put up for downloafd. I’ll send the Mochi guys another one today.
Paul,
Thanks!
All others:
Sorry about the files. It looks like the wrong set got posted. I sent Mochi an old .zip I’m not sure how I made that mistake. I’ve sent Mochi a new batch. so hopefully they will be up soon.
Thanks,
-Steve
Hi, and thx for sharing your methods,
My (very personal, I admit) opinion about programming games is that the code should be the simplest possible.
While the techniques you showcase (EventDispatch, implementing UI stuff programmatically) deserve to be known, they are not absolutely required when programming a simple game.
Much of the UI stuff can be done in the timeline almost without code, for example. Regarding game mechanics, standard method calling may be more efficient than event dispatching.
From a learning standpoint, the result may be that some users think it’s quite complex.
Flash has this strange property that implemeting a simple button can be done in about 100 different ways ;-), then it’s always good to share techniques (seems that flashers love doing that)!
olive
Olive,
You are right, there are dozens of ways to do most anything in AS2, and nothing I have presented is absolutely required. However, I have programmed many games, and the methods I showcase are the ones I have developed that that save the most time when maintaining the code, updating, the code, adding new features, and using the code as basis for additional games, all the while maintaining performance.
Hi everyone, the correct files should be posted now. Thanks!
eagerly waiting for next lesson
I just wanted to state that f in front of your state functions is an example of “Apps Hungarian Notation” and the f stands for function. Very good tutorial by the way!! I enjoyed it :)
Justin, I think yo uare correct…which means the prefix is absolutely useless in this context. It should probably be “o” for object. Just goes to prove that you shouldn’t blindly follow a convention just because other developers you know do it, especially if you don’t know the reason for doing it!
…by the way, I just finished the code for the next lesson today, and I’m writing the text now. I hope to have it completeld
shortly.
So, where do I go to download all the sources?
Btw, I am a fifteen year old who, like you at childhood, wanted to make video games. I got Flash CS3 and even Photoshop CS3, but I am just getting started on programming, and I hardly can keep up with this guide. Is this begginer orientated or should I try something easier?
jimsotonna…
jimsotonna dropped by…
free quotes…
Excellent post. Keep it up!…
I am new to Flash programming, and find it difficult to follow this tutorial. Is there a download of the entire project that I could examine?
How do you set the path for objects in library folders? If I don’t put MovieClips in a folder then Flash can find them.
This is not a beginner guide and not an advanced guide…
If you can’t follow this than you should use another tutorial(I started with a few tuts from emanueleferonato.com).
nice guide any way
Realize I’m coming to this somewhat late, but I got to say, Steve – I paid my way through a Master’s Degree in IT, and this tutorial taught me more in substantially less time! Outstanding work, and remarkably generous of you to share your knowledge. I found myself repeatedly hitting a sort of mental block when it came to Flash until reading this tutorial.
I’m extremely grateful to you. I’ve purchased video tutorials on Flash that were less thorough and less approachable. You’re a gentleman and a scholar, sir! =D
I wish you’d start with more basic basic’s. For those of us who have never programmed flash; what do we need? Is it possible to do so without forking over $800 to Adobe?