## Tutorial: Create a Brick Breaker Game in AS3 – Part 3

Written By MrSun at 1:25 pm - Wednesday, July 30th, 2008
Categories: Beginner Tutorials, Flash

### Step 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:

```//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
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:
//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);

Game Frame:
stop();
//VARIABLES
//These variables are needed for moving the ball
var ballXSpeed:Number = 8; //X Speed of the Ball
var ballYSpeed:Number = 8; //Y Speed of the Ball
//First I defined a function where all of
//the code needed to start the game is placed
//This includes listeners, variable definitions, and other stuff
function beginCode():void{
//runs a function every time a frame passes
//Adds a listener to the ball which
//runs a function every time a frame passes
//making the level
makeLvl();
}

//Keeping the paddle in the stage

//If the mouse goes off too far to the left
}
//If the mouse goes off too far to the right
if(mouseX > stage.stageWidth - mcPaddle.width / 2){
}
}

function moveBall(event:Event):void{
//Code for moving ball goes here
mcBall.x += ballXSpeed; //Move the ball horizontally
mcBall.y += ballYSpeed; //Move the ball vertically
//Bouncing the ball off of the walls
if(mcBall.x >= stage.stageWidth-mcBall.width){
//if the ball hits the right side
//of the screen, then bounce off
ballXSpeed *= -1;
}
if(mcBall.x <= 0){
//if the ball hits the left side
//of the screen, then bounce off
ballXSpeed *= -1;
}
if(mcBall.y >= stage.stageHeight-mcBall.height){
//if the ball hits the bottom
//then bounce up
ballYSpeed *= -1;
}
if(mcBall.y <= 0){
//if the ball hits the top
//then bounce down
ballYSpeed *= -1;
}
calcBallAngle();
}
}

function calcBallAngle():void{
//ballPosition is the position of the ball is on the paddle
var ballPosition:Number = mcBall.x - mcPaddle.x;
//hitPercent converts ballPosition into a percent
//All the way to the left is -.5
//All the way to the right is .5
//The center is 0
var hitPercent:Number = (ballPosition / (mcPaddle.width - mcBall.width)) - .5;
//Gets the hitPercent and makes it a larger number so the
//ball actually bounces
ballXSpeed = hitPercent * 10;
//Making the ball bounce back up
ballYSpeed *= -1;
}

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
And the final product:

Source .fla File
```