PDA

View Full Version : addEventListener for arrays


Lenster921
07-21-2008, 10:45 PM
So I got what I wanted to do with one star at a time falling when I hit a block. The problem now is that I can't seem to get the star to continue falling when the program moves to animate another star falling. Only one star can be falling at a time, how can i change this so that the star falls off the screen?
package {
import flash.display.*;
import flash.events.*;
import flash.utils.getTimer;
import flash.geom.Rectangle;
import flash.text.TextField;

public class MyPaddleBall extends MovieClip {
private const ballRadius:Number = 9;
private const wallTop:Number = 38;
private const wallLeft:Number = 18;
private const wallRight:Number = 532;
private const paddleY:Number = 780;
private const paddleWidth:Number = 90;
private const starRadius:Number = 15;
private const paddleCurve:Number = .005;
private const paddleHeight:Number = 18;
private static const pointsForHit:int = 10;
private static const pointLOT:int = -100;

//key ovjwxra
private var paddle:MyPaddle;
private var paddlez:MyPaddle;
private var ball:MyBall;
private var star:MC4
private var ballSpeed:Number;
private var gameScore:int;
private var Level:int;
//bricks
private var bricks:Array;
var stars:Array;
//ball velocity
private var ballDX:Number;
private var ballDY:Number;

//animation timer

private var lastTime:uint;
//number of balls left
var balls:Number;
private var MCTween:MovieClip;
// initialize arrow variables
var gravity:Number = .05;
var starDY:Number = 0;
//make collection of bricks
var lastTimez:int;
public function startMyPaddleBall() {

//create paddle
paddlez = new MyPaddle();
paddle = new MyPaddle();
paddle.y = paddleY;
paddlez.y = paddleY
addChild(paddle);
addChild(paddlez);

star = new MC4();
//createbricks
makeBricks();
balls = 4;
gameMessage.text = "Click To Start";

//set up animation
lastTime = 0
addEventListener(Event.ENTER_FRAME,moveObjects);
stage.addEventListener(MouseEvent.CLICK,newBall);


//score
gameScore = 0;

showGameScore();
//level
Level = 1;
//speed
ballSpeed = .2;

}
public function showGameScore() {
gameScoreField.text = "Score: " +String(gameScore);
}
public function makeBricks(){
bricks = new Array();
//create a grid of bricks 5 by 8
for(var y:uint=0;y<10;y++) {
for(var x:uint=0;x<8;x++){
var newBrick:MyBrick = new MyBrick();
newBrick.x = 60*x+65;
newBrick.y = 20*y+50;
addChild(newBrick);
bricks.push(newBrick);
}
}

}


public function moveStars(){

}


public function newBall(event:Event){
//don't go here if there is already a ball
if (ball !=null)return;
gameMessage.text = " ";

//create ball, center it
ball = new MyBall();
ball.x = (wallRight-wallLeft)/2+wallLeft;
ball.y = (paddleY-wallTop)/2+wallTop
addChild(ball);

//ball velocity
ballDX= 0
ballDY= ballSpeed;
//use up a ball
ballsLeft.text="Balls: "+balls;
//reset animation
lastTime = 0;
}

// set event listeners

public function moveObjects(event:Event) {
movePaddle();
moveBall();

}
public function makeStars(){
stars = new Array();
//create a grid of bricks 5 by 8
for(var y:uint=0;y<10;y++) {
for(var x:uint=0;x<8;x++){
var newStar:MC4 = new MC4();
newStar.x = 60*x+65;
newStar.y = 20*y+50;
newStar.speedY = 8;
stars.push(newStar);
}
}}
function enterFrameHandler (e:Event) {
//Loop through the numbers
for (var i = 0; i< stars.length; i++ ) {
//Update the y position
if (stars[i].y > 800){
stars[i] == null;
}
stars[i].y += stars[i].speedY;



}
}


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;
}
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.bottom<=paddleRect.top){
if(newBallRect.right>paddleRect.left){
if(newBallRect.left<paddleRect.right){
if(oldBallRect.top<paddleRect.top){
if(newBallRect.top<paddleRect.top){
//bounce back
newBallY -= 2*(newBallRect.bottom - paddleRect.top);
ballDY*= -1;
//decide new angle
ballDX = (newBallX - paddle.x)*paddleCurve;
}

}
}
}
} else if(newBallRect.top > 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.left) {
newBallX += 2*(brickRect.left - oldBallRect.right);
ballDX *= -1;
} else if (oldBallRect.left>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);
}
//removethebrick
makeStars();
addChild(stars[i]);
addEventListener(Event.ENTER_FRAME,enterFrameHandl er);

removeChild(bricks[i]);



gameScore += pointsForHit;
showGameScore();
bricks.splice(i,1);

if (bricks.length < 79)
{
clearWindow();
nextLevel();

return;
}
}
}
//set new position
ball.x = newBallX;
ball.y = newBallY;

var starRect = new Rectangle(star.x-starRadius,star.y-starRadius,starRadius*2,starRadius*2);
var paddlezRect = new Rectangle(paddlez.x-paddleWidth/2,paddlez.y-paddleHeight/2,paddleWidth,paddleHeight);
if(paddlezRect.intersects(starRect)){
if(starRect.bottom >= paddlezRect.top){

gameScore += pointsForHit;
trace("ball");
}}
}
function clearWindow() {
//remove paddle and bricks
for(var i:int=bricks.length-1;i>=0;i--){
removeChild(bricks[i]);
}
removeChild(paddle);
removeChild(paddlez);

stars = null;
paddle = null;
bricks = null;
paddlez = null;
//remove ball
if (ball !=null) {
removeChild(ball);
ball = null;
}


//remove listeners
removeEventListener(Event.ENTER_FRAME,moveObjects) ;
stage.removeEventListener(MouseEvent.CLICK,newBall );
removeEventListener(Event.ENTER_FRAME,enterFrameHa ndler);
}
function endGame(){
gotoAndStop("gameover");
}

function nextLevel(){
ballSpeed +=.025;
trace(ballDY++);
Level++;
gotoAndStop("play");

//create paddle
paddlez = new MyPaddle();
paddle = new MyPaddle();
paddle.y = paddleY;
paddlez.y = paddleY
addChild(paddle);
addChild(paddlez);
//createbricks
makeBricks();
gameMessage.text = "Level: " +Level;

//set up animation
lastTime = 0
addEventListener(Event.ENTER_FRAME,moveObjects);
stage.addEventListener(MouseEvent.CLICK,newBall);

//make collection of bricks

showGameScore();

}

}

}