Solved

AS3 Android game reset trouble

Posted on 2011-03-06
9
572 Views
Last Modified: 2012-05-11
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;

          }
       }

    }

Open in new window

0
Comment
Question by:cubical38
  • 3
  • 3
9 Comments
 
LVL 2

Accepted Solution

by:
nycynik earned 500 total points
ID: 35142985
Answer Your Question:

You have all the bricks that are broken to be replaced.

This line is moving the bricks off the screen.

               
 TweenMax.to(broken, 1, {y:stage.stageHeight + (broken.width * 2), ease:Quart.easeIn, onCompleteListener:(Event.ENTER_FRAME, countIt)});

Open in new window


The problem is, your not moving them back in your end game.


Hints:

That said, you probably do not want to create the bricks the way you are now, using flash.  Each brick should be a instance of a movie clip from the library (create a brick, make it a symbol, add it to the library and delete it off the stage), that you add in your start game with code.  This way, you can reconfigure the bricks for other levels, and reset them when the game starts.

First:
So you need a method that places instances of the bricks where you like them.  This is a simple for loop that used some method (an array?) to determine the x/y coordinates of the bricks to start the game/level.  It is alos how many there are.  Psudo Code:

// this shoudl be in setbricks instead of what you have now.
for (var idx:int=0;idx<total_bricksthislevel; idx++) {
    var newbrick = new brick_mc(); // <-- you create a mc in the library, and set it to export to actionscript, and name it brick_mc
    newbrick.x = startlocationX[idx];
    newbrick.y = startlocationY[idx];
    // here you should add all the bricks to an array to track them and reset them.
   addChild(newbrick);
   newbrick.addEventListener(Event.ENTER_FRAME, brickCollision);

   

}

Open in new window


Second: at the end of the game, or start of a new game, loop over the array you create in the comment above, and remove all the bricks.  Then run that code again to re-create them.


Hope that helps
0
 

Author Comment

by:cubical38
ID: 35148018
I had something similar to this originally using columns and rows to fill the array of bricks.  My initial reason for placing the bricks on the stage (I wouldnt normally do) was to make it easier to create multiple brick layouts of different stacking and or placement.  Although that causes a major issue in the start over or move to next level.  Im thinking maybe I could set a max row and a max column for each level and have the array return the bricks in a math.random then just increase brick and row numbers per level.  Thoughts?  I think we have an answer, just thought you might have some insight on this idea.

Thanks for the reply and thoughts...
0
 
LVL 2

Assisted Solution

by:nycynik
nycynik earned 500 total points
ID: 35150536
I think that you could increase the number of rows each time.

I would hard code arrays to hold the bricks.  If you have different kinds of bricks, you could assign each kinda a number, then store them in the array.

so something like this

var brickSetup:Array = new Array();
brickSetup[0] = [0,1,1,1,0];
brickSetup[1] = [1,1,1,1,1];
brickSetup[2] = [0,1,1,1,0];

--

Then you can access it using brickSetup[0][0]

If its a 1, place a brick, if its a 0, no brick.  Of course you have to calculate the location of the brick, so, assuming the thing is 400px wide, and you want a gap at both sides of say 20px, and you have 5 bricks across (like shown in the data above), then to calcluate the spot would be

var location:number = 20+(brickidx*(brickwidth+brickMargin));

remember to loop from 1 to whateer,

for (var bulidItrows:int=1; bulidItrows:int<NumberofBricksWide;bulidItrows:int++) {
     for (var bulidItcols:int=1; bulidItcols<NumberofBricksWide;bulidItcols++) {
             var locationcols:number = 20+(bulidItcols*(brickwidth+brickMargin));
             var locationrows:number = (bulidItrows*(brickheight+brickMargin));

             // place the brick here


     }
}
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:cubical38
ID: 35150667
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!
0
 
LVL 2

Assisted Solution

by:nycynik
nycynik earned 500 total points
ID: 35150884
I mean, to find the value of brick should go there, or not, use that line, like so:

var BrickKind = brickSetup[bulidItrows][bulidItcols];

2nd thing:

There is a small typo in the above code too:

for (var bulidItrows:int=1; bulidItrows:int<NumberofBricksWide;bulidItrows:int++) {

Open in new window


should read


for (var bulidItrows:int=1; bulidItrows<NumberofBricksWide;bulidItrows++) {

Open in new window


3rd thing

One more thing, when you define the arrays, you could do this:

0=no brick
1=standard brick
2=special brick

(of course you can have many more special bricks.

Even if that was only used for colors, it might be fun.

Mike


0
 

Author Comment

by:cubical38
ID: 35150969
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...
0
 
LVL 37

Expert Comment

by:CyanBlue
ID: 36978228
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now