I am following a tutorial to teach me how to make a brick break clone and for some reason when all of the bricks are broke the ball will not reset like it is suppose to. Here is my code,
Frame 1 (Frame)
//Current level player is on
var currentLvl:Number = 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);
Frame 2 (Frame)
stop();
//how many lives you got
var lives:Number = 3;
//if the game is over
var gameOver:Boolean = false;
//These variables are needed for moving the ball
var ballXSpeed:Number = 10;
//X Speed of the Ball
var ballYSpeed:Number = 10;
//Y Speed of the Ball
mcBg.onRelease = function() {
//creating a function that will run when bg is clicked
//move the bg out of range so it isn't bothersome
mcBg._x = 800;
_root.onEnterFrame = function() {
//this function will run during every single frame
//The paddle follows the mouse
mcPaddle._x = _xmouse-mcPaddle._width*.5;
//If the mouse goes off too far to the left
if (_xmouse<mcPaddle._width/2) {
//Keep the paddle on stage
mcPaddle._x = 0;
}
//If the mouse goes off too far to the right
if (_xmouse>Stage.width-mcPaddle._width/2) {
//Keep the paddle on stage
mcPaddle._x = Stage.width-mcPaddle._width;
}
//MOVING THE BALL
mcBall._x += ballXSpeed;
mcBall._y += ballYSpeed;
//Bouncing the ball off of the walls
if (mcBall._x>=Stage.width-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.height-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;
}
if (mcBall.hitTest(mcPaddle)) {
//calculate the ball angle if ball hits paddle
calcBallAngle();
}
//checking if the bricks are all gone
if (brickAmt == 0) {
//reset the level by increasing the level
currentLvl++;
//and re-running makeLvl
makeLvl();
//reset the ball's position
mcBall._x = 44.4;
mcBall._y = 294.9;
//then move the background back
mcBg._x = 0;
//and remove all of these events
_root.onEnterFrame = null;
}
};
};
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:Number = _root.lvlArray[currentLvl-1].length;
//the current row of bricks we are creating
var brickRow:Number = 0;
//Now, creating a loop which places the bricks onto the stage
for (var i = 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
_root.attachMovie('mcBrick', 'brick'+i, _root.getNextHighestDepth());
//setting the brick's coordinates via the i variable and brickRow
_root['brick'+i]._x = 15+(i-brickRow*7)*75;
_root['brick'+i]._y = 10+brickRow*20;
//giving this brick some actions
_root['brick'+i].onEnterFrame = function() {
if (this.hitTest(_root.mcBall)) {
//if this touches the ball
//then destroy this mofugger!
this.removeMovieClip();
//and make the ball bounce away
ballYSpeed *= -1;
}
};
//checks if the current brick needs a new row
for (var c = 1; c<=10; c++) {
if (i == c*7-1) {
brickRow++;
}
}
}
}
}
makeLvl();
Everything as far the instances go exist, trust me. But the code I used said it was suppose to reset after I knocked every brick down but it doesn't. =/ Any suggestions?
Thanks!
-Zeus