## 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 addChild(brick); } } }```

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

#### Game Frame:

```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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{ //Adds a listener to the paddle which //runs a function every time a frame passes mcPaddle.addEventListener(Event.ENTER_FRAME, movePaddle); //Adds a listener to the ball which //runs a function every time a frame passes mcBall.addEventListener(Event.ENTER_FRAME, moveBall); //making the level makeLvl(); }   function movePaddle(event:Event):void{ //The paddle follows the mouse mcPaddle.x = mouseX - mcPaddle.width / 2; //Keeping the paddle in the stage   //If the mouse goes off too far to the left if(mouseX < mcPaddle.width / 2){ //Keep the paddle on stage mcPaddle.x = 0; } //If the mouse goes off too far to the right if(mouseX > stage.stageWidth - mcPaddle.width / 2){ //Keep the paddle on stage mcPaddle.x = stage.stageWidth - mcPaddle.width; } }   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; } //Hitting the paddle if(mcBall.hitTestObject(mcPaddle)){ 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<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 addChild(brick); } } } beginCode();```