Game Development Reference
In-Depth Information
Making the Balloons
In many games, you will need to have some kind of logic that makes things happen without user
interaction. In an action game like Balloon Saw, this logic usually centers around creating
enemies that they player must fight. The timing, number, and placement of these enemies are the
main ingredients in adding both challenge and difficulty to the game. As you might imagine, there
are many ways to tackle the job of creating these objects. These methods range from completely
random to exactly the same every time the game is played. For Balloon Saw, we are using a
completely random method. However, throughout this topic, we will introduce many different
ways to accomplish this task, each suited to a particular game.
The
makeEnemies()
function performs a simple test to see if a balloon needs to be created on a
frame. The frequency of balloons is controlled by a random value and the current game level.
First, we retrieve a random number between 0 and 99 by calling
Math.floor(Math.random()
*100)
and put that into the
chance
variable. Next, we test
chance
to see if it is less than
2 + level
(e.g.,
3
on level 1,
4
on level 2, or
5
on level 3). As the level increases, the chance for an enemy
balloon to be created increases as well.
When we drop into the
if
statement, it is time to create a balloon to put on the screen. We do
this by creating a temporary variable named
tempEnemy
that we will configure. First, we create a
new instance of
EnemyImage()
and set
tempEnemy
to its value. Next, we set the
speed
, which we
create as a dynamic variable. Flash
MovieClip
objects can be assigned new variables at
runtime, and that is a very flexible feature. However, the trade-off is that they take more system
memory then a regular class property. We are using one here because doing so is easy for this
example and we don't have to create a separate
Enemy
class (something we will do later in this
topic). We set the
speed
dynamic variable to
3 + level
, which means that as the
level
increases, the balloons will move up the screen faster, thus making the game a bit harder.
Our
EnemyImage
MovieClip
has five frames, each with a different colored balloon. We randomly
choose one of these colors and jump to it using
tempEnemy.gotoAndStop
(Math.floor(Math.random()*5)+1);
. These colors are is just for effect, but in a more elaborate
game, different colored balloons could be used for things such as bonuses or extra chances.
Next, we set the
x
and
y
values for the initial placement of the balloon. The
y
value is always
435
, which means it will start just off the bottom of the screen. The
x
value is a random number
between
0
and
514
. The balloon graphic in
EnemyImage()
is
35
pixels across, and the
registration point is set to
0,0
. This will allow the balloons to appear at a random
x
location but
still remain fully on the screen. If we used a random value up to
550
(the width of the Flash
movie), some balloons could appear partly or completely off the screen. For a game that tracks
how many objects a player has missed, this would be unfair. Finally, we use
addChild()
to put
the new balloon into the display list and push it into the
enemies
array so we can track it during
the game.
public function makeEnemies() {
var chance:Number = Math.floor(Math.random() *100);
if (chance < 2 + level) {
var tempEnemy:MovieClip;
tempEnemy = new EnemyImage()
tempEnemy.speed = 3 + level;
tempEnemy.gotoAndStop(Math.floor(Math.random()*5)+1);
tempEnemy.y = 435;
tempEnemy.x = Math.floor(Math.random()*515)
addChild(tempEnemy);