cubical38
asked on
AS3 Android game reset trouble
Here it is, I have a simple brick breaker game in progress, yet I am starting to pull hairs on the rest game / start over function. My class is here, and everything thing seems to be working except for the endGame() function. Goal, not quite reached: once the endGame() funtion is fired, reset everything as if nothing had even happened (game over, reset from beginning). Once I have this function in tact I can begin to add levels, difficulty etc. As always I am open to better implementations of all functions, and any help is always appreciated as criticism only makes the "Tiger Blood" thicker.
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.display.DisplayObject;
import flash.geom.Point;
import flash.utils.Timer;
import flash.utils.Timer;
import flash.utils.getTimer;
import flash.events.TimerEvent;
import com.greensock.*;
import com.greensock.easing.*;
public class collision extends MovieClip
{
private var ball:Ball = new Ball();
private var paddle:Paddel = new Paddel();
private var ballXSpeed:Number = 28;
private var ballYSpeed:Number = 28;
private var timer:Timer = new Timer(20,999999);
private var columns:int = 6;
private var rows:int = 2;
private var spacer:int = 5;
private var brickAmt:Number = 10;
private var fallenBricks:Number = 0;
private var len:int = numChildren;
private var startButton:word = new word();
private var nextButton:comp = new comp();
private var offset:Number;
public function collision()
{
addChild(startButton);
addChild(paddle);
paddle.x = stage.stageWidth / 2 - (paddle.width / 2);
paddle.y = stage.stageHeight - (paddle.height/2);
startButton.cover.buttonMode = true;
startButton.visible = true;
nextButton.cover.buttonMode = true;
nextButton.visible = false;
startButton.y = stage.stageHeight / 2;
startButton.x = stage.stageWidth / 2;
paddle.addEventListener(MouseEvent.MOUSE_DOWN, paddleDrag, false, 0, true);
startButton.addEventListener(MouseEvent.CLICK, beginGame);
}
public function beginGame(e:MouseEvent):void
{
startButton.removeEventListener(MouseEvent.CLICK, beginGame);
nextButton.removeEventListener(MouseEvent.CLICK, beginGame);
startButton.visible = false;
nextButton.visible = false;ball.addEventListener(Event.ENTER_FRAME, moveBall);
ball.y = paddle.y - (ball.height * 2);
ball.x = paddle.x + (paddle.width / 2) - (ball.width / 2);
setBricks();
}
function paddleDrag(event:MouseEvent):void
{
trace(paddle.y);
offset = paddle.x - event.stageX;
stage.addEventListener(MouseEvent.MOUSE_MOVE, performDrag, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_UP, paddleDrop, false, 0, true);
}
function paddleDrop(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, performDrag);
stage.removeEventListener(MouseEvent.MOUSE_UP, paddleDrop);
}
function performDrag(event:MouseEvent):void
{
//TweenMax.to(paddle, .2, {x:event.stageX + offset, ease:Expo.easeOut});
if (mouseX < paddle.width / 2)
{
paddle.x = 0;
}
else if (mouseX > stage.stageWidth - paddle.width / 2)
{
paddle.x = stage.stageWidth - paddle.width;
}
else
{
paddle.x = event.stageX + offset;
}
}
public function moveBall(e:Event):void
{
addChild(ball);
ball.x += ballXSpeed;
ball.y += ballYSpeed;
if (ball.x <= 0 || ball.x >= stage.stageWidth - ball.width)
{
ballXSpeed *= -1;
}
if (ball.y <= 0)
{
ballYSpeed *= -1;
}
if (ball.y >= paddle.y)
{
endGame();
trace(len);
}
if (ball.hitTestObject(redBrick))
{
endGame();
trace(len);
}
if (ball.hitTestObject(paddle))
{
ballAngle();
}
createTrailBall();
}
public function setBricks():void
{
for (var i:int=0; i<len; i++)
{
var brick:Object = getChildAt(i);
if (brick is MovieClip)
{
brick = MovieClip(brick);
brick.addEventListener(Event.ENTER_FRAME, brickCollision);
}
}
}
public function brickCollision(e:Event):void
{
var broken:MovieClip = e.currentTarget as MovieClip;
var count:Number = 0;
if (ball.hitTestObject(broken))
{
ballYSpeed *= -1;
TweenMax.to(broken, 1, {y:stage.stageHeight + (broken.width * 2), ease:Quart.easeIn, onCompleteListener:(Event.ENTER_FRAME, countIt)});
broken.removeEventListener(Event.ENTER_FRAME, brickCollision);
//trace("hit");
//broken.addEventListener(Event.ENTER_FRAME, countIt);
}
}
public function countIt(e:Event):void
{
if (len == 1)
{
trace("chicken dinner");
e.currentTarget.removeEventListener(Event.ENTER_FRAME, countIt);
endGame();
}
else
{
trace(e.currentTarget);
len--;
trace(len);
e.currentTarget.removeEventListener(Event.ENTER_FRAME, countIt);
}
}
public function endGame():void
{
trace("end game = " + len);
ball.removeEventListener(Event.ENTER_FRAME, moveBall);
addChild(nextButton);
nextButton.visible = true;
nextButton.y = stage.stageHeight / 2;
nextButton.x = stage.stageWidth / 2;
nextButton.addEventListener(MouseEvent.CLICK, beginGame);
}
public function createTrailBall(e:Event=null):void
{
var trailBall:Ball=new Ball();
trailBall.x = ball.x;
trailBall.y = ball.y;
trailBall.addEventListener(Event.ENTER_FRAME,animateTrailBall);
addChildAt(trailBall,0);
TweenMax.to(trailBall, .6, {colorTransform:{tint:0xFFFF00,tintAmount:1}});
TweenMax.to(trailBall, .6, {blurFilter:{blurX:20, blurY:20}});
}
public function animateTrailBall(e:Event):void
{
e.target.alpha -= 0.05;
e.target.scaleY -= 0.04;
e.target.scaleX -= 0.04;
if (e.target.alpha < 0)
{
e.target.removeEventListener(Event.ENTER_FRAME,animateTrailBall);
removeChild((MovieClip)(e.target));
}
}
public function ballAngle():void
{
var ballPosition:Number = ball.x - paddle.x;
var hitPercent:Number = (ballPosition / (paddle.width - ball.width)) - .5;
ballXSpeed = hitPercent * 10;
ballYSpeed *= -1;
}
public function brickAngle():void
{
ballYSpeed *= -1;
}
}
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Makes sense. I will implement it tonight.
one question what do you mean here: Then you can access it using brickSetup[0][0]
Thanks Again!
one question what do you mean here: Then you can access it using brickSetup[0][0]
Thanks Again!
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Ahh of coarse! Okay when I get in front of it tonight I will set it all up. Thanks Again! I dont see any reason this wont work but Ill wait til tonight to award points, just in case...
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
ASKER
Thanks for the reply and thoughts...