ForumsProgramming ForumHow to pause a game?

6 2973
rjbman
offline
rjbman
215 posts
Nomad

So I'm working on my first game. Mostly it's guess and check as I'm learning as I program it. My question is how to pause it.

This is what I currently have:

mcPause.addEventListener(MouseEvent.MOUSE_DOWN, pausegame);
function pausegame(event:MouseEvent):void {
txtStart.text="Click to unpause";
mcPaddle.removeEventListener(Event.ENTER_FRAME, movePaddle);
mcBall.removeEventListener(Event.ENTER_FRAME, moveBall);
gameRunning=false;
stage.addEventListener(MouseEvent.CLICK, resumeGame);

}
function resumeGame(event:MouseEvent):void {
txtStart.text="";
mcPaddle.addEventListener(Event.ENTER_FRAME, movePaddle);
mcBall.addEventListener(Event.ENTER_FRAME, moveBall);
gameRunning=true;
stage.removeEventListener(MouseEvent.CLICK, resumeGame);
}

In this, mcPause is my pause button. mcPaddle is the paddle, and mcBall is the ball. Both of those have event listeners that move them.

When I try to pause, nothing happens. If I change the mcPause listener to a MOUSE_DOWN the pause works, but only if you hold down over it, rather than clicking it.

If it's possible, I'd also like to have &quot" on the keyboard pause/unpause it.

If anyone can give me help with either of these, I'd be much obliged.
  • 6 Replies
plasmafish
offline
plasmafish
252 posts
Nomad
PixelSmash
offline
PixelSmash
566 posts
Nomad

What's the code for resuming? So far I can't see that much wrong with your code - if that's all I can't think of a reason why it shouldn't work. Which is why I'm guessing you somehow fire the resume function pretty soon after. You might want to check with a few traces here and there.
What you could do in any case, to prevent any errors, is removing the event listener you set earlier for the pause in that function (function pauseGame -> removeEventListener(MouseEvent.MOUSE_DOWN etc)

Do you by any chance use the MouseEvent.MOUSE_UP as resume? That would cause your problems. If you did what I wrote earlier in this post, you could use the pause function to add a mouse down listener that fires the resume. This way you can prevent firing these functions when you don't want them to!

Good luck!

rjbman
offline
rjbman
215 posts
Nomad

The second function is how I resume. I see what you're saying though; I'll try it when I get on flash again.

rjbman
offline
rjbman
215 posts
Nomad

I think it may have something to do with the fact that it clicks, then registers the click as resume game. I'll try adding a delay.

PixelSmash
offline
PixelSmash
566 posts
Nomad

Might be... what does the code look like that makes the button execute the resume function?

rjbman
offline
rjbman
215 posts
Nomad

Alright, so I got it to work. Basically, I did an if statement; only problem is that you do have to click on the actual button instead of anywhere on screen. I'll post the code Monday; it's at my school so I don't have access.

Showing 1-6 of 6