How to Create a Brick Breaker Game in AS3 – Part 3

Part 3: Setting Up the Bricks on Stage

Ok, we’ve got the paddle and the ball. Now, the only major thing left to program is the brick. This is also the hardest to program, so it will be split up into 2 parts, setting them up, and breaking them down. Let us begin.

First of all, we need to look at how we’re going to set the bricks on the stage. We’re going to use an array with different numbers that will signify different bricks. In this tutorial, because we are keeping it simple, we will only use 2 different numbers, 1 and 0. 1 will mean to place a brick, and 0 will mean not to. An example for a simple level would be this:

//this would create one row of bricks
lvl1Array:Array = new Array(1,1,1,1,1,1,1);

Of course, this code will do nothing unless we create a function to actually make the bricks. The first step to this is actually making the brick movieclip. So, mine is just going to be a plain white, with dimensions of 70×15 pixels. Also, this movieclip should be exported for ActionScript so we can dynamically add it to the stage. Got it? Now, here comes the tricky part, programming it. First of all, we are going to define a function called makeLvl(). Then, we are going to need to make some starting variables. These variables, however, can’t be reset every time the user beats a level, so it we have to make a frame before the the frame with all of the code. If you don’t get it, then here’s a picture. In this frame, put the following code:

 1 2 3 4 5 6 //Current level player is on var currentLvl:int = 1; //The array code for lvl 1 var lvl1Code:Array = new Array(1,1,1,1,1,1,1); //The array that contains all of the level codes var lvlArray:Array = new Array(lvl1Code);

Hopefully, I’ve explained what each of the variables do well enough that you understand. Also, in the 2nd frame, the game frame, just add a stop(); to the beginning. This way, the game doesn’t keep on looping.

Now, we’re going to go back to the game farme and we are going to add some intense code into the makeLvl() function. Here we go:

function makeLvl():void{ //Places bricks onto Level
//finding the array length of the lvl code

//The index has to be currentLvl-1 because:
//array indexes start on 0 and our lvl starts at 1
//our level will always be 1 higher than the actual index of the array
var arrayLength:int = lvlArray[currentLvl-1].length;
//the current row of bricks we are creating
var brickRow:int = 0;
//Now, creating a loop which places the bricks onto the stage
for(var i:int = 0;i<arrayLength;i++){
//checking if it should place a brick there
if(lvlArray[currentLvl-1][i] == 1){
//creating a variable which holds the brick instance
var brick:MovieClip = new mcBrick();
//setting the brick's coordinates via the i variable and brickRow
brick.x = 15+(i-brickRow*7)*75;
brick.y = 10+brickRow*20;
//checks if the current brick needs a new row
for(var c:int = 1;c<=10;c++){
if(i == c*7-1){
brickRow ++;
}
}
//finally, add the brick to stage
}
}
}

Now, just put makeLvl(); into the beginCode() function and you’re set. The next step is getting the ball to break the bricks. But, that is for a later time. Here is the code you should have:

First Frame:

 1 2 3 4 5 6 //Current level player is on var currentLvl:int = 1; //The array code for lvl 1 var lvl1Code:Array = new Array(1,1,1,1,1,1,1); //The array that contains all of the level codes var lvlArray:Array = new Array(lvl1Code);