|
Learn how to make a Flash game within minutes
with no prior scripting
knowledge!
This step-by-step guide will show you how to make an entertaining
Flash game quickly and easily
This article is for people who are just beginning to use Flash tools to create
interactive stuff. This article will take you from a blank flash document
to an action game in minutes, with any tricky things explained in ordinary,
everyday language that you can skip over if you want.
As a special thankyou for reading this article I’m giving the complete
source of game.fla, which you can use for whatever purposes you want, private
or commercial. You must however give credit to GameInnovator.com for using any
part of the source in your projects. This flash file was made with Flash MX
2004.
The complete source can be downloaded here.
So without further ado, let us fire up our favourite Flash tool. There are
many tools available, so I will use Macromedia Flash as it is the most universally
used tool.
Use Sothink SWF Quicker to create flash
applications and movies. This program is like an easy to use version of Macromedia
Flash, and I have used this versatile tool to easily make a variety of applications.
Making Flash Games is Only the Tip of the Iceberg
Flash games are all fine to make, however there is so much more you can do with them.
I recommend seeing the
online
video course "How to Make Money from Video Games" to find out how you can make
real money from games.
Learn How to Make Real Money from Video Games Now
Setting the scene
Create a new flash document (menu File/New) and save it as game.fla. This is
the design file that will contain the design and content for the game.
If you get stuck anywhere on this tutorial there is a finished game.fla at
the bottom of this page that can be downloaded to see what things should look
like.
Change the document properties (menu Modify/Document) so that the dimensions
are 640 by 480 and so that the frame rate is 30 frames a second. The background
colour is assumed to be white.
We will start by creating the 5 initial objects needed to make the game work.
Draw the player’s ship, the alien, the player’s projectile, the
alien’s laser and an explosion on the same frame and layer.
Turn each drawing into a Movie Clip with its own instance name. I will explain
below in more detail how this is done. More advanced people can skip to the
naming of the instances.
How to turn a drawing into a Movie Clip:
1. Select the drawing by dragging a box around it.

2. Convert to a Movie Clip by going to the menu and then Modify or Insert
depending on the Flash version. Choose “Convert to Symbol”. Alternatively
you could use the shortcut by pressing F8.
3. Set the behaviour as Movie Clip and choose an appropriate name. This name
is not the instance name. Click here
for an illustration.
4. Select the new Movie Clip and in the properties panel at the bottom set
the instance name to whatever is needed.
The instance names are below:
The Movie Clip for the player’s ship is called PlayerShip
The Movie Clip for the alien is called Alien
The Movie Clip for the player’s projectile is called PlayerShotT
The Movie Clip for the alien’s laser is called AlienShotT
The Movie Clip for the explosion is called ExplosionT
Notice how there is a T at the end of certain instance names. This is a convenience
to indicate that objects with a T at the end are to be used as templates for
copies of themselves. This will be explained later.
Click here for a screenshot of a
possible set up scene.
Let’s make things happen
We’ll create some basic behaviours for our Movie Clip objects next.
At the moment the player’s ship isn’t too responsive. We’re
going to change that.
Left click on the player’s ship to select it. Go to the Actions
panel at the bottom and enter in the following code in the white part on the
right:
onClipEvent(enterFrame)
{
if(Key.isDown(Key.RIGHT)) {
//this will move the player 15 units to the right
this._x = this._x + 15;
} else if (Key.isDown(Key.LEFT)) {
//this will move the player 15 units to the left
this._x = this._x - 15;
}
}

I’ll give some explanation below as to what this code means. Feel free
to skip ahead of this explanation if you don’t want to get heavily involved
in ActionScript.
A bit about ActionScript:
onClipEvent(enterFrame){} This is basically a container that’s
called a function. This container holds code to be run within it. The function
isn’t executed or run until something else tells it to. “onClipEvent”
is the name of the function. Some names such as “onClipEvent”
are reserved by Flash in order for certain things to run properly.
The text in between the two brackets “(“ and
“)”are any parameters that are passed to the
function. Parameters are passed from the source that executed it. There can
be more than one parameter eg “(var1,test2)”. These parameters
can usually be used within the function. Parameters will be described in more
detail later.
The stuff in between curly braces “{” and ”}”
is the actual code within the function that is executed.
Things like “this._x= this._x + 15;” are called statements.
Statements are bits of code that tells something to do something else. There
is usually at most one statement every line. Every statement must end with
“;” so that Flash knows when the statement ends.
Comments are ignored by Flash, but help the coder know what
their code is doing. Comments begin with a “//” and continue for
the entire line.
if(Key.isDown(Key.RIGHT)) {} This is called an if statement.
This is basically a test to see if the stuff within the brackets is true.
If the stuff is true then the code within the curly braces is run.
else if (Key.isDown(Key.LEFT)) {} This can only go after the “if”
statement or another “else if” statement. Basically, if the stuff
in the previous test is false, then the stuff in this “else
if” statement is tested. If the test in the brackets is true
than the code within the braces is executed.
This little piece of code will let you move the ship around from left to right.
Preview your flash animation (CTR-ENTER) and try moving the ship with the left
and right arrow buttons.
The ship can move outside the boundaries so we need to add in a bit more code
to restrict its movement.
Change the line:
if(Key.isDown(Key.RIGHT )) {
to:
if(Key.isDown(Key.RIGHT) and this._x < 640) {
Also, change the line:
} else if (Key.isDown(Key.LEFT)) {
to:
} else if (Key.isDown(Key.LEFT) and this._x > 0) {
Preview your animation again. This time the player ship should stay within the
boundaries.
Now would be a great time to place the player’s ship around the bottom
of the stage, as it will be firing upwards.
Time to breathe some life into the alien. Select the alien and put this code
into the ActionScript part:
//this is triggered when the clip loads
onClipEvent (load)
{
xmove = random(15) - random(15); //comments can also
go after statements
ymove = random(15) - random(15); //this sets the ymovement
randomly
this._x = random(640); //this sets the x and y position
randomly
this._y = random(480);
}
//this is triggered everytime a new frame is entered, in this case 30 times
a sec
onClipEvent (enterFrame)
{
//lets move the alien around
this._x = this._x + xmove;
this._y = this._y + ymove;
//bounce the alien if it is out of bounds
if(this._x > 640){xmove = Math.abs(xmove) * -1}
if(this._x < 0){xmove = Math.abs(xmove)}
if(this._y > 480){ymove = Math.abs(ymove) * -1}
if(this._y < 0){ymove = Math.abs(ymove)}
}
Try running the game now. The alien should select a random direction and speed
and bounce around the stage.
You should move the explosion, alien laser and player projectile off of the
stage. This is because these are the templates that will be copied whenever
necessary and so they will remain throughout the entire game.
Setting the score
Lets add some texts to display the score. Select the text tool and create two text objects in one of the corners, stacked on top of eachother. Write "Score:" in the top one and "High Score:" in the bottom one.
Make sure that they are long enough to accomodate the text along with the score in a single line.
Change the properties in both text objects to be dynamic text. Set the "var" property to score in the score text and highscore in the highscore text.
The "var" property is the name of the root variable that the text objects will pull their data from to display. For example, if the score variable is "score: 50" then that's what the score text will display.
A new hope
Time to give our ship some offensive weaponry.
Select the frame that you are working in. This should be the first one on the
left.

Go to the Actions panel at the bottom and insert the following code:
//The stuff below this will only be executed once on this frame
//This will set the score and highscore to 0 on application start
scorenumber = 0;
highscorenumber = 0;
//This will create the arrays which basically stores stuff in numbered slots
Explosions = new Array();
BadShots = new Array();
PlayerShots = new Array();
//This is to create the unique IDs
PShotID = 0;
BShotID = 0;
ExplosionID = 0;
//This is to create the playershot timer, so that it only shoots when ready
PShotTimer = 0;
//This one is required because each instance works on its own "layer"
//Instances cant be on the same layer, hence the use of a unique ID
UniqueID = 10;
//This is to stop from entering the next frame in our timeline
stop();
//This stuff runs 30 times a second
this.onEnterFrame = function()
{
//timer goes down everytime the frame is entered again
if(PShotTimer > 0){
-- PShotTimer;
}
//test for space key down
if(Key.isDown(Key.SPACE))
{
if(PShotTimer < 1)
{
//increment
the unique id and id for playershots
PShotID
= PShotID + 1;
UniqueID
= UniqueID + 1;
//loop
back to 1 if there are many shots exisiting
if
(PShotID == 10) {PShotID = 1}
//run
function to make shot
PlayerShots[PShotID]=
MakeObject("PShot",PlayerShotT,UniqueID,PlayerShip._x,PlayerShip._y);
//Set
the timer so that the player must wait 10 frames before firing again
PShotTimer
= 10;
}
}
}
//function to work out distance between objects
md = function (x1, y1, x2, y2)
{
x = x1 - x2
y = y1 - y2
var thing =Math.sqrt((x * x) + (y * y))
if (thing == 0) {thing = 999}
return(thing);
}
//making instances or "copies" of the templates
MakeObject = function(instname,template,id,x,y)
{
// make temporary variable
var temp;
//assign name
var name = instname + id;
//make new instance from template
var temp = template.duplicateMovieClip(name, id);
//set its x and y coordinates
setProperty (name, _x, x);
setProperty (name, _y, y);
//return the object to whatever called this function
return temp;
}
Double-click on your explosion that you made. Now convert the explosion again
into a Symbol and choose Graphic. You should now have a Graphic
within a MovieClip for your explosion.
Create an animation to your liking that basically plays out the explosion from
start to finish. On the last frame, put this code in:
this.onEnterFrame = function()
{
this.removeMovieClip();
}
This will remove the explosion once it finishes its animation. Click here
for an example.
This won’t however remove the original explosion, only the instance.
This is why you should keep this object away from the stage so that it is not
seen.
Go back to your scene by clicking something along the lines of Scene1 on the
top left.

Select the player’s projectile and insert the following code:
//this is triggered when the clip loads
onClipEvent (load)
{
xmove = random(5) - random(5); //comments can also
go after statements
ymove = (random(5) + 15) * -1; //this sets the ymovement
randomly
}
//this is triggered everytime a new frame is entered, in this case 30 times
a sec
onClipEvent (enterFrame)
{
//lets move the player's shot around
this._x = this._x + xmove;
this._y = this._y + ymove;
//destroy this if it reaches the top
if(this._y < 0){this.removeMovieClip();}
//test to see if this is colliding with the alien
col = _root.md(_x, _y, _root.Alien._x, _root.Alien._y)
//you can change 40 to another number if shots seem
to pass through
if(col < 40)
{
//increments the IDs and creates
an explosion
//notice that _root. is put
in front
++ _root.UniqueID
++ _root.ExplosionID
_root.Explosions[_root.ExplosionID]
= _root.MakeObject("Explosion",_root.ExplosionT,_root.UniqueID,_root.Alien._x,_root.Alien._y);
//"resets" the alien
_root.Alien._x = random(640)
_root.Alien._y = random(480)
_root.Alien.xmove = random(15)
- random(15); //comments can also go after statements
_root.Alien.ymove = random(15)
- random(15); //this sets the ymovement randomly
//increases the score by one and updates the score variable for the text
++_root.scorenumber;
_root.score = "Score " + _root.scorenumber; //puts a string and number together
//destroys this instance
//once the instance is gone,
no further actions can be performed
this.removeMovieClip();
}
}
Run the game now and use SPACEBAR to shoot. By now your ship
moves around, shoots and blows up the alien on a direct hit. Only a couple more
pieces of code left!
The alien strikes back
These aliens don’t come in peace, so select the alien and insert this
code at the bottom, but before the end “}” brace:
//this is to get the alien to fire randomly
fire = random(20);
//one in 20 chance of firing every frame!
if(fire == 1)
{
_root.BShotID = _root.BShotID
+ 1;
_root.UniqueID = _root.UniqueID
+ 1;
//run function to make shot
_root.BadShots[_root.BShotID]=
_root.MakeObject("BShot",_root.AlienShotT,_root.UniqueID,_root.Alien._x,_root.Alien._y);
}
The bottom of the alien’s code should look something like this:

Finally, select the alien’s laser and add this code:
//this is triggered when the clip loads
onClipEvent (load)
{
xmove = random(5) - random(5); //comments can also
go after statements
ymove = (random(5) + 15); //this sets the ymovement
randomly
}
//this is triggered everytime a new frame is entered, in this case 30 times
a sec
onClipEvent (enterFrame)
{
//lets move the player's shot around
this._x = this._x + xmove;
this._y = this._y + ymove;
//destroy this if it reaches the top
if(this._y > 480){this.removeMovieClip();}
//test to see if this is colliding with the player
col = _root.md(_x, _y, _root.PlayerShip._x, _root.PlayerShip._y)
//you can change 40 to another number if shots seem
to pass through
if(col < 25){
//increments the IDs and creates an explosion
//notice that _root. is put in front
++ _root.UniqueID
++ _root.ExplosionID
_root.Explosions[_root.ExplosionID] = _root.MakeObject("Explosion",_root.ExplosionT,_root.UniqueID,_root.PlayerShip._x,_root.PlayerShip._y);
//"resets" the player's ship
_root.PlayerShip._x = random(640)
//checks to see if score greater than highscore, then updates
if(_root.scorenumber > _root.highscorenumber)
{
_root.highscorenumber = _root.scorenumber;
_root.highscore = "High Score: " + _root.highscorenumber;
}
//resets the score to 0 and updates text
_root.scorenumber = 0;
_root.score = "Score " + _root.scorenumber;
//destroys this instance
//once the instance is gone, no further actions can be performed
this.removeMovieClip();
}
}
Congratulations, you now have a functional game! Of course, this article barely
scratches the surface of what is possible with Flash.
Addicting games of some complexity can be made with the wonderful tool known
as flash, and as your first glimpse into Flash game design
I hope it has been enjoyable.
As for what to do next, I suggest adding in a nice background in a layer underneath
the current layer to get rid of the dull white, and perhaps to examine some
of the commented code that has been used.
I hope you have enjoyed following this guide as much as I have enjoyed writing
it.
After you have had a chance to sit back and absorb this article, please send
us your comments and suggestions - your feedback is greatly appreciated.
Want to make some real money from video games? You’re going to need the right information, or else your efforts will fall flat.
This online video course along with the included bonuses gives you the information you need to steer away from potential hazards and get you on the path to making real money.
Stop blundering into the same obstacles everyone else does when the vital information you need is at your fingertips.
Learn how to make real money from video games
Written By Damien D
The source code is free for private and commercial purposes. However, no part
of this page can be reproduced on another website without the written permission
from GameInnovator.com
|