# AS3 Brick breaker start menu problems

**URL:** <https://forum.kirupa.com/t/as3-brick-breaker-start-menu-problems/329337>\
**Category:** programming\
**Created:** [August 15, 2012, 11:33am UTC](https://forum.kirupa.com/t/as3-brick-breaker-start-menu-problems/329337 "2012-08-15T11:33:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kingsage18](https://avatars.discourse-cdn.com/v4/letter/k/ec9cab/32.png) [@kingsage18](https://forum.kirupa.com/u/kingsage18)\
**Post date:** [August 15, 2012, 11:33am UTC](https://forum.kirupa.com/t/as3-brick-breaker-start-menu-problems/329337/1 "2012-08-15T11:33:13Z")

</div>

Hi

I’m new to this forum and I registered simply to ask for on this problem.  
I’m making a brick breaker game and everything seems to work except that when you either loose the game (the ball hits the ground 3 times you fail) or win the menu glitches out. the play button on the menu doesn’t appear and when you click it just takes you to the instructions menu.

Can anyone help me, if the problem is easy enough then feel free to fix it other wise i just need to know where I’m screwing up.

thanks

action frame 1

stop();  
var soundFile:URLRequest=new URLRequest(“commoditize.mp3”);  
var snd:Sound = new Sound();  
snd.load(soundFile);  
var sndChannel:SoundChannel = new SoundChannel();  
sndChannel = snd.play();  
sndChannel.addEventListener(Event.SOUND\_COMPLETE, onComplete);

function onComplete(e:Event):void {  
sndChannel = snd.play();  
}

Import bricks frame

//IMPORTS  
//import Brick;  
//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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,0);  
//The array that contains all of the level codes  
var lvlArray:Array = new Array(lvl1Code);

ball movement frame

stop();  
//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  
var brickAmt:int = 0;  
//how many lives you got  
var lives:int = 3;  
//if the game is over  
var gameOver:Boolean = false;  
var wingame:Boolean = false;  
//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(event:MouseEvent):void{  
stage.removeEventListener(MouseEvent.CLICK, beginCode);  
//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);  
//adding a listener to check if the level is done  
addEventListener(Event.ENTER\_FRAME, checkLevel);

}

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 &lt; mcPaddle.width / 2){
             //Keep the paddle on stage
             mcPaddle.x = 0;
     }
     //If the mouse goes off too far to the right
     if(mouseX &gt; 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;  
mcBall.y += ballYSpeed;  
//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;  
lives --;  
//if there aren’t any lives left  
if(lives \<= 0) {  
//the game is over now  
gameOver = true;  
//got to a lose frame  
gotoAndStop(‘lose’);  
}  
}  
if(mcBall.y \<= 0){  
//if the ball hits the top  
//then bounce down  
ballYSpeed \*= -1;  
}  
//This code should be placed in the moveBall() function  
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&lt;arrayLength;i++){
             //checking if it should place a brick there
             if(lvlArray[currentLvl-1]* == 1){
                      //creating a variable which holds the brick instance
                      var brick:Brick = new Brick();
                      //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&lt;=10;c++){
                              if(i == c*7-1){
                                        brickRow ++;
                              }
                      }
                      //finally, add the brick to stage
                     addChild(brick);
             }
     }

```

}

function checkLevel(event:Event):void{  
//checking if the bricks are all gone  
if(brickAmt == 0){  
//reset the level by increasing the level  
currentLvl ++;  
//and re-running makeLvl  
makeLvl();  
//then resetting the ball’s and paddle’s position  
mcBall.x = 150;  
mcBall.y = 265;  
mcPaddle.x = 230;  
//then removing all of the listeners  
mcPaddle.removeEventListener(Event.ENTER\_FRAME, movePaddle);  
mcBall.removeEventListener(Event.ENTER\_FRAME, moveBall);  
removeEventListener(Event.ENTER\_FRAME, checkLevel);  
//then listening for a mouse click to start the game again  
stage.addEventListener(MouseEvent.CLICK, beginCode);  
}  
}

//if the mouse clicks, then begin the game  
stage.addEventListener(MouseEvent.CLICK, beginCode);  
//makin the level  
makeLvl();

[U]start menu frame

[/U]stop();

bplay.addEventListener(MouseEvent.CLICK, startGame);

function startGame(event:MouseEvent):void{  
bplay.addEventListener(MouseEvent.CLICK, startGame);  
gotoAndPlay(3);  
}

bhowto.addEventListener(MouseEvent.CLICK, howTo);

function howTo(event:MouseEvent):void{  
bhowto.addEventListener(MouseEvent.CLICK, howTo);  
gotoAndPlay(‘how to’);  
}

How to frame

stop();

//resetting the game if the mouse is clicked  
bmenu.addEventListener(MouseEvent.CLICK, mEnu);

function mEnu(event:MouseEvent):void{  
//removing this listener  
bmenu.addEventListener(MouseEvent.CLICK, mEnu);  
//resetting the game  
gotoAndPlay(‘start’);  
}

you lose frame

//The lose frame

//resetting the game if the mouse is clicked  
stage.addEventListener(MouseEvent.CLICK, resetGame);

function resetGame(event:MouseEvent):void{  
//removing this listener  
stage.addEventListener(MouseEvent.CLICK, resetGame);  
//resetting the game  
gotoAndPlay(‘start’);  
}

(actual .fla file is to big to upload)
