Create a Flash ball game with visual from above tutorial part 2
Filed Under Flash •
December 13th update: 3rd part released
January 16th update: 4th part released
This is the second part of the tutorial about the creation of a Flash ball game with visual from above.
I suggest you to read part 1 before reading this post. Here I will start adding features to our game as said in the I Had to do it post.
Let's start with the creation of the first, and most important, tile:
Exit tile
Well, an exit tile means there is an exit, and if there is an exit there is another level, and if there is another level I need a way to code levels.
I stored all levels in a function, in this way:
-
function draw_level(number) {
-
level = new Array();
-
switch (number) {
-
case 1 :
-
_root.ball_start_x = 0;
-
_root.ball_start_y = 0;
-
level[0] = new Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0);
-
level[1] = new Array(1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1);
-
level[2] = new Array(1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1);
-
level[3] = new Array(1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1);
-
level[4] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
-
level[5] = new Array(1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1);
-
level[6] = new Array(1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1);
-
level[7] = new Array(1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1);
-
level[8] = new Array(1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1);
-
level[9] = new Array(1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1);
-
level[10] = new Array(0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1);
-
break;
-
case 2 :
-
_root.ball_start_x = 0;
-
_root.ball_start_y = 0;
-
level[0] = new Array(1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0);
-
level[1] = new Array(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1);
-
level[2] = new Array(1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1);
-
level[3] = new Array(1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1);
-
level[4] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
-
level[5] = new Array(1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1);
-
level[6] = new Array(1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1);
-
level[7] = new Array(1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1);
-
level[8] = new Array(1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1);
-
level[9] = new Array(1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1);
-
level[10] = new Array(0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1);
-
break;
-
}
-
_root.createEmptyMovieClip("bricks", 2);
-
bricks._x = 240-(80*ball_start_x);
-
bricks._y = 220-(80*ball_start_y);
-
for (y=0; y<=10; y++) {
-
for (x=0; x<=11; x++) {
-
if (level[y][x]>0) {
-
depth = y*12+x;
-
place_brick = bricks.attachMovie("brick", "brick_"+depth, bricks.getNextHighestDepth(), {_x:x*80, _y:y*80});
-
place_brick.gotoAndStop(level[y][x]);
-
}
-
}
-
}
-
}
The function accepts one parameter, that will be the level number. In this two-level game, the switch at line 3 loads level 1 (lines 4-18) or level 2 (lines 19-33).
You will notice at lines 5, 6, 20 and 21 two new declarations. ball_start_x and ball_start_y store the starting vertical and horizontal position of the ball in the level.
Lines 36-37 Places the ground according to ball_start_y and ball_start_x position. Remember that in this game the ball remain fixed in the middle of the screen while the background is scrolling.
The rest of the code is explained at part 1, except for line 43 that I will explain later.
Now we have more than 1 level. As much as 2!!
Before we proceed, let me introduce another feature you will get a lot familiar with:
The dead
Since there are several ways to die, I need a function to call everytime the player dies.
-
function ball_die() {
-
bricks._x = 240-(80*_root.ball_start_x);
-
bricks._y = 220-(80*_root.ball_start_y);
-
xspeed = 0;
-
yspeed = 0;
-
draw_level(lev);
-
}
It's very easy... when the player dies, the ball position is restored to its initial position and both xspeed and yspeed are set to zero. Then redraws the level.
Very, very easy.
Now it's time to explain how to design tiles.
Look at this picture:

As you can see, the brick movieclip has several frames (10 in the example). Every frame contains a brick type, and has a stop(); in it to... ehm... stop it at the current frame. Now it should be clear why I wrote that line 43 in the level drawing function: I goto and stop the brick instance according to the value stored in the level array.
Now it's time to design some nasty levels and explain how does it work.
In this tutorial, I'll design levels 5 bricks tall and 5 bricks wide, but there is (almost) no limit to levels size you can create.
It's interesting anyway the complexity of some levels, even if so small.
Here it is the actionscript, all in the 1st frame as real PROs...
-
_root.attachMovie("starz", "starz", 1, {_x:-20, _y:-20});
-
_root.attachMovie("ball", "ball", 3, {_x:240, _y:220});
-
ball.texture.setMask(ball.ball_itself);
-
yspeed = 0;
-
xspeed = 0;
-
lev = 1;
-
draw_level(lev);
-
ball.onEnterFrame = function() {
-
friction = 0.99;
-
power = 0.4;
-
brick_x = Math.floor((bricks._x-200)/80)*-1;
-
brick_y = Math.floor((bricks._y-180)/80)*-1;
-
type_of_tile = level[brick_y][brick_x];
-
switch (type_of_tile) {
-
case 1 :
-
// normal tile
-
break;
-
case 2 :
-
// down spin tile
-
yspeed += 0.2;
-
break;
-
case 3 :
-
// up spin tile
-
yspeed -= 0.2;
-
break;
-
case 4 :
-
// left spin tile
-
xspeed -= 0.2;
-
break;
-
case 5 :
-
// right spin tile
-
xspeed += 0.2;
-
break;
-
case 6 :
-
// glass tile
-
depth = brick_y*12+brick_x;
-
bricks["brick_"+depth]._alpha--;
-
if (bricks["brick_"+depth]._alpha<1) {
-
level[brick_y][brick_x] = 0;
-
}
-
break;
-
case 7 :
-
// spin tile
-
xspeed *= 1.05;
-
yspeed *= 1.05;
-
break;
-
case 8 :
-
// slip tile
-
friction = 1;
-
power = 0;
-
break;
-
case 9 :
-
// beam
-
depth = brick_y*12+brick_x;
-
if (bricks["brick_"+depth].lava._currentframe>90) {
-
ball_die();
-
}
-
break;
-
case 10 :
-
// exit
-
lev++;
-
_root.removeMovieClip("bricks");
-
draw_level(lev);
-
break;
-
default :
-
// hole
-
ball_die();
-
break;
-
}
-
if (Key.isDown(Key.LEFT)) {
-
xspeed -= power;
-
}
-
if (Key.isDown(Key.RIGHT)) {
-
xspeed += power;
-
}
-
if (Key.isDown(Key.UP)) {
-
yspeed -= power;
-
}
-
if (Key.isDown(Key.DOWN)) {
-
yspeed += power;
-
}
-
xspeed *= friction;
-
yspeed *= friction;
-
if ((xspeed<0.1) and (xspeed>-0.1)) {
-
xspeed = 0;
-
}
-
if ((yspeed<0.1) and (yspeed>-0.1)) {
-
yspeed = 0;
-
}
-
bricks._y -= yspeed;
-
bricks._x -= xspeed;
-
starz._x = -20+((bricks._x-240)/10);
-
starz._y = -20+((bricks._y-220)/10);
-
this.texture._y += yspeed;
-
this.texture._x += xspeed;
-
if (this.texture._x>53) {
-
this.texture._x -= 63;
-
}
-
if (this.texture._x<-53) {
-
this.texture._x += 63;
-
}
-
if (this.texture._y>53) {
-
this.texture._y -= 63;
-
}
-
if (this.texture._y<-53) {
-
this.texture._y += 63;
-
}
-
};
-
function ball_die() {
-
bricks._x = 240-(80*_root.ball_start_x);
-
bricks._y = 220-(80*_root.ball_start_y);
-
xspeed = 0;
-
yspeed = 0;
-
draw_level(lev);
-
}
-
function draw_level(number) {
-
level = new Array();
-
switch (number) {
-
case 1 :
-
_root.ball_start_x = 0;
-
_root.ball_start_y = 0;
-
level[0] = new Array(1, 1, 1, 1, 10);
-
level[1] = new Array(0, 0, 0, 0, 0);
-
level[2] = new Array(0, 0, 0, 0, 0);
-
level[3] = new Array(0, 0, 0, 0, 0);
-
level[4] = new Array(0, 0, 0, 0, 0);
-
break;
-
case 2 :
-
_root.ball_start_x = 0;
-
_root.ball_start_y = 0;
-
level[0] = new Array(1, 4, 4, 5, 0);
-
level[1] = new Array(0, 0, 0, 1, 0);
-
level[2] = new Array(0, 0, 0, 1, 0);
-
level[3] = new Array(0, 0, 0, 10, 0);
-
level[4] = new Array(0, 0, 0, 0, 0);
-
break;
-
case 3 :
-
_root.ball_start_x = 0;
-
_root.ball_start_y = 0;
-
level[0] = new Array(1, 6, 6, 4, 0);
-
level[1] = new Array(0, 0, 0, 6, 0);
-
level[2] = new Array(6, 5, 5, 6, 0);
-
level[3] = new Array(6, 0, 0, 0, 0);
-
level[4] = new Array(1, 1, 10, 0, 0);
-
break;
-
case 4 :
-
_root.ball_start_x = 0;
-
_root.ball_start_y = 0;
-
level[0] = new Array(1, 7, 0, 0, 0);
-
level[1] = new Array(0, 7, 0, 7, 10);
-
level[2] = new Array(1, 3, 0, 1, 0);
-
level[3] = new Array(1, 0, 0, 1, 0);
-
level[4] = new Array(1, 1, 1, 7, 0);
-
break;
-
case 5 :
-
_root.ball_start_x = 4;
-
_root.ball_start_y = 2;
-
level[0] = new Array(7, 8, 8, 8, 10);
-
level[1] = new Array(1, 0, 0, 0, 0);
-
level[2] = new Array(1, 8, 8, 3, 1);
-
level[3] = new Array(0, 0, 0, 0, 0);
-
level[4] = new Array(0, 0, 0, 0, 0);
-
break;
-
case 6 :
-
_root.ball_start_x = 2;
-
_root.ball_start_y = 2;
-
level[0] = new Array(2, 8, 9, 8, 9);
-
level[1] = new Array(9, 0, 0, 0, 1);
-
level[2] = new Array(2, 8, 1, 0, 3);
-
level[3] = new Array(0, 0, 0, 0, 4);
-
level[4] = new Array(10, 9, 9, 8, 6);
-
break;
-
case 7 :
-
_root.ball_start_x = 2;
-
_root.ball_start_y = 2;
-
level[0] = new Array(0, 0, 0, 0, 0);
-
level[1] = new Array(0, 0, 0, 0, 0);
-
level[2] = new Array(0, 0, 1, 0, 0);
-
level[3] = new Array(0, 0, 0, 0, 0);
-
level[4] = new Array(0, 0, 0, 0,