PaddleBall + Falling Apple Game
I'm trying to incorporate 2 games into one. I want to take the paddle ball game and add a function that when the ball hits a brick, the brick falls down and can be caught by a secondary paddle at the bottom of the screen that follows the now free moving first paddle.
I can't get the brick to fall it just stays up there and becomes a transparent sprite. The function that I added was "animateBrick". If anyone wants to see the full source file let me know I'll email it to them.
package {
// set event listeners
public function moveObjects(event:Event) {
movePaddle();
moveBall();
animateBrick();
}
public function movePaddle(){
//match horizontal value with the mouose
var newX:Number = Math.min(wallRight-paddleWidth/2,
Math.max(wallLeft+paddleWidth/2,
mouseX));
paddle.x = newX;
paddlez.x = newX;
var newY:Number = Math.min(800-paddleHeight/2,
Math.max(200+paddleWidth/2,mouseY));
paddle.y = newY;
}
function animateBrick() {
var lastTimez:int = getTimer();
var timeDiff:int = getTimer()-lastTimez;
lastTimez += timeDiff;
brickDY += gravity*timeDiff;
bricks.y += timeDiff*brickDY;
}
public function moveBall() {
//don't go here if in between balls
if (ball == null) return;
//get new location for ball
if (lastTime == 0) lastTime = getTimer();
var timePassed:int = getTimer() - lastTime;
lastTime += timePassed;
var newBallX = ball.x + ballDX*timePassed;
var newBallY = ball.y + ballDY*timePassed;
var oldBallRect = new Rectangle(ball.x-ballRadius,ball.y-ballRadius,ballRadius*2,ballRadius*2);
var newBallRect = new Rectangle(newBallX-ballRadius, newBallY-ballRadius,ballRadius*2, ballRadius*2);
var paddleRect = new Rectangle(paddle.x-paddleWidth/2,paddle.y-paddleHeight/2,paddleWidth,paddleHeight);
//colission with paddle
if(newBallRect.bottom >=paddleRect.top){
if(oldBallRect.bottompaddleRect.left){
if(newBallRect.left 800){
removeChild(ball);
ball = null;
balls--;
if (balls >0) {
gameMessage.text = "Click For Next Ball";
gameScore += pointLOT;
if(gameScore < 0){
gameScore = 0;
}
showGameScore();
}else {
gameScore += pointLOT;
if(gameScore < 0){
gameScore = 0;
}
showGameScore();
clearWindow();
endGame();
}
return;
}
}
//collision with left wall
if (newBallRect.left < wallLeft){
newBallX += 2*(wallLeft - newBallRect.left);
ballDX *= -1;
}
if (newBallRect.right > wallRight){
newBallX += 2*(wallRight - newBallRect.right);
ballDX *= -1;
}
if (newBallRect.top < wallTop){
newBallY+=2*(wallTop - newBallRect.top);
ballDY*=-1
}
//collison with bricks
for(var i:int=bricks.length-1;i>=0;i--){
//get brick rectangle
var brickRect:Rectangle = bricks[i].getRect(this);
//isthere a brick collision
if(brickRect.intersects(newBallRect)) {
//ball hitting left or right side
if (oldBallRect.right brickRect.right){
newBallX += 2*(brickRect.right - oldBallRect.left);
ballDX *= -1;
}
//ball hitting top or bottom
if (oldBallRect.top> brickRect.bottom){
ballDY *= -1;
newBallY += 2*(brickRect.bottom-newBallRect.top);
}else if (oldBallRect.bottom < brickRect.top) {
ballDY *= -1;
newBallY += 2*(brickRect.top - newBallRect.bottom);
}
//move ball
//removethebrick
/*removeChild(bricks[i]);*/
/*/* bricks[i].gotoAndPlay(star);
var starRect = new Rectangle(ball.x-ballRadius,ball.y-ballRadius,ballRadius*2,ballRadius*2);
var paddlezRect = new Rectangle(paddlez.x-paddleWidth/2,paddlez.y-paddleHeight/2,paddleWidth,paddleHeight);
if(starRect.bottom > paddlezRect.top){
removeChild(star);
gameScore+= pointsForHit;
}*/
gameScore += pointsForHit;
showGameScore();
bricks.splice(i,1);
if (bricks.length < 48)
{
clearWindow();
nextLevel();
return;
}
}
}
//set new position
ball.x = newBallX;
ball.y = newBallY;
}
Copyright Gary Rosenzweig
