1
votes

During early development of my game I did not really regard how the game will be paused in case the player gets interrupted. I figured it shouldn't be anything too difficult so I left it as something I would look into once I got the core of the game finished.

I'm here now and trying to figure out a way to pause my game is very difficult, from my point of view anyway.

As of now I followed a few tutorials to get to where I am today, mainly tutorials from ASGamer. The preloader and menu screens tutorial that can be found at that site is what I used and so far I've had a few problems which I will ask later.

Anyway I find it difficult to wrap my head around how I would be able to pause or suspend game play when the user presses a designated key. I guess I should explain how my game functions.

I have 3 frames in my main timeline.

First frame is for the preloader, simple.

Second frame is where the game is. It has my asset holder and all my scripts run when on that frame.

Third frame is stop(); action.

Once the preloader finishes my the "Menu Screens" starts, this is the tutorial I followed. It loads and unloads movieclips with UnLoad() and Load().

I use Load() to load the main menu which happens at the beginning, then use UnLoad() to unload the current menu then load the next designated menu, for example Main Menu -> the Game I use Unload(new GameMenu(stage)); The stage is constantly passed back and forth with these menus so it gets confusing sometimes.

In the GameMenu is where I run the game from, I made a class GamePlay that extends MovieClip. From there it spawns the players and enemies and plays as a normal game, this is where my problem begins.

First I tried to create a key handler to get key presses from the user. At one point it worked the key presses were registering but after I made some changes to other scripts the key presses were not recognized any more. So I moved the key presses to my Main.as which is the main script that my fla uses. The keyboard events work fine, but now actually stopping the game...how do I even begin? I would have to stop all movieclips and eventlisteners until the user presses the designated pause button again, how would I accomplish this?

I read somewhere that using state machines is the best bet for having a pause in any game, but I'm not sure how to do this but I'm sure there are tutorials and blogs I can read to make a state machine, but if I did add one or make one would I have to redo all my code to accommodate state machines?

2
Might want to post this on: gamedev.stackexchange.comTim Cooper
A lot of Flash games base most of their animation/physics on an ENTER_FRAME event. So when the game needs to be paused, the listener is removed.user1385191

2 Answers

3
votes

Ideally your game is going to have one single game loop that controls all of the action - most flash gaming frameworks will provide this for you. Somewhere, something is listening for Event.ENTER_FRAME and running logic on each frame.

So, if you've built your game using a framework or using one central point of access to Event.ENTER_FRAME, it's easy - you can either remove the listener, call the framework's built-in pause function or make your loop listener check for a "isPaused" Boolean flag.

If, however, you're not using a framework and you just have a lot of different objects that don't really know about each other and each is listening for enter frame on its own then you might have a problem. I think you'd essentially have to go through and implement pausing functionality on each one, which would be rough.

I hope that helps!

0
votes

One way to alleviate the animation problem is to hide your game screen when the game is paused (open a "game paused" window over the main game screen), so you save yourself the bother of stopping all the animations. If you prefer stopping the animations, the simplest solution I can think of is registering all animating objects in an array, so you can stop them all and play them all again in a simple loop. Not elegant, but better than refactoring your game for a pause function.

You will definitely need to keep a "isGameRunning" boolean variable, which you'll check that is true in user input functions (keyboard events and mouse events), game logic functions (enter frame) and probably before playing sounds. This variable will save you the trouble of removing and re-adding events.