Lesson 1: Game Setup
Part 1: Setting Up Your .fla File
To begin with, you will need to know how to set-up your .fla file for maximum efficiency when designing and programming your game. I’ve found that the best strategy for this is as follows:
1. Choose a sensible layer and frame strategy

For this game we will use as few frames as possible. In fact, the actual game will only use one frame in Flash (”Game”). However, we will want to make sure the timeline is as clear as possible to develop and maintain the code in the game.
Layers:
Labels: This layer is used to place any “jump” labels” that you will use for your game .fla. We will try avoid this for any actual game logic, but for they will be necessary for easy integration of services like Mochiads, Mochibot, and a logo screen.
Code: You should always only have one layer in a Flash movie that contains ActionScript. Otherwise, it will be very difficult to find code that is spread all around the Flash movie.
Assets: Any and all movie clips, graphics, buttons, etc. should go on the layers below the top two. The Assets layer above if for “exported assets” which I will explain a bit later.
2. Put as much code as possible in external .as files
It is much easier to maintain a Flash game that used external .as files for most the code, and for one that uses any sort of Object-Oriented structure, it is vital. For Lesson 1 we will have the following .as files: HomeWars.as, Player.as, Missile.as, TitleScreen.as, EndScreen.as. In subsequent lessons we will add even more. Each .as file for our game holds a seperate object oriented class that will be used to create the game.
3. Set your game to at least 31 frames per second (FPS)
31 FPS is the bare minimum for games in Flash. 30+ FPS will give your games a smooth feel, and it will allow your code enough cycles to process movement, hits, etc properly. However, this must be followed-up with efficient code. However, no matter how many FPS you select, you must write code that is efficient enough to actually allow for that many FPS. It does not matter how high you set your FPS if the processor is pegged at 100% while your game is running.The setting is flash is what the player will “attempt” to run the game at, but there are no guarantees.
4. Use folders in your library

You should always attempt to organize your Flash assets in your library in some way that will make them easy to find when you need to update them, make code changes, or debug your game. I like to use the structure above. No matter how you organize your library though, be sure it makes sense to you.
The organization of the library is not only a “nice to have,” it should reflect the object-oriented structure of your code. You will notice that the Object folder above contains a series of MovieClips that all start with the letter “F” and all say something like “Export:Fxxx” in the Linkage column. All of the objects in this folder are associated with an ActionScript class.
Let’s take a closer look at the FPlayer MovieClip. By right-clicking (CTRL-click on Mac) the MovieClip in the library, and clicking the “properties” option you will see a dialog box similar to the following:

In the Linkage section, you will see:
- Identifer. This is the name we will use in our code to create an instance of one of these objects.
- AS 2.0 Class. This is the object class that will hold all the code and properties for this object. The convention is to name your .as file the same as your class name, so the Player class would be in an external .as file name Player.as .
- Linkage check boxes. Make sure that Export For ActionScript is checked. This allows use make use of this object in our code. However, also notice that Export In First Frame is not checked. This is also not the default behavior in Flash. Having this unchecked means that it will be necessary to include an instance of this object in a frame somewhere before we use it. While that might seem like a small burden, the benefits outweigh it. It is necessary to un-check Export In First Frame if you want any kind of pre loader counter or loading bar to work properly in your game. We will do something similar for any sounds and music we use in the game.
Now, remember the set-up of our frames?

Notice the “Assets” label on Frame 17. This is where we will place all of our exported assets. We will use a simple “gotoAndStop("Game")” from the Preloader frame to jump-over this frame, so it is never seen in the game. However, it will serve our purposes, allowing for all exported assets to be available before they are needed, but after the preloader does it’s preloading magic
On the assets layer on Frame 17, you will see what looks like a mess:

This contains one of each object we will be using in the game. The look of this frame does not matter because, again, no one will ever see it, but its function is vital.
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?