0

I am coding a flash game in which the ball hits a movie clip object and this takes the user to a new scene.

this works fine but when i return back to the game scene the ball moves twice as fast.

How/Where do i reset the variables when returning to the game. See below for how i set the variables at the moment

var XSpeed:Number = 9;
var YSpeed:Number = 9;

Any help would be greatly appreciated. Thanks.

EDIT****************************************

Ok so i think i phrased this question pretty badly here is my relevant code:

var XSpeed:Number = 9; //X Speed of the Ball
var YSpeed:Number = 9; //Y Speed of the Ball


function beginCode():void{

mcPaddle.addEventListener(Event.ENTER_FRAME, movePaddle);

mcBall.addEventListener(Event.ENTER_FRAME, moveBall);

mcBall.addEventListener(Event.ENTER_FRAME, changeFrame);
}

beginCode() is then called at the end. As previously mentioned i think the problem is that the event listener already exists and is being called again each time the user returns to the page. I have tried to add an if statement to check (see comments below) but this has not worked...

Any help would be greatly appreciated. Thanks.

2 Answers2

0

Well, that depends entirely on how your game is written and how you expect it to work.

For your particular issue it's as simple as doing this when the ball hits a movie clip:

XSpeed = 9;
YSpeed = 9;
Marty
  • 39,033
  • 19
  • 93
  • 162
0

That sounds like this question. The problem there was that an event listener was being added again when then game restarted, making the game loop fire twice as often (and everything happen twice as fast).

You'll either need to remove the listener when the game ends, or add a check to make sure it's only added once.

Community
  • 1
  • 1
David Mear
  • 2,254
  • 2
  • 13
  • 21
  • if(Ball2.hasEventListener(Event.ENTER_FRAME)) Ball2.removeEventListener(Event.ENTER_FRAME, moveBall2); else Ball2.addEventListener(Event.ENTER_FRAME, moveBall2); – user1982108 Jan 18 '13 at 02:08
  • One way to do this is `addEventListener(Event.REMOVED_FROM_STAGE, cleanup)`, and in your `cleanup` function, remove the event listener that is being fired twice. – Amy Blankenship Jan 18 '13 at 02:22