Search code examples
actionscript-3frame-rate

Actionscript 3: Pausing a game


Hey so I'm working on pausing my game. I have it almost working, but there's a couple malfunctions that I can't seem to understand.

Basically I have some code (below) that pauses the frame rate and then resumes the frame rate. Both snippets of code work correctly individually, but when put together, if I press "p" it instantly cycles through both snippets of code, effectively only executing the second one (I put traces in and it shows that both codes are being executed when I press "p" once.)

So my first and primary question is how do I get this to work? I just want to execute one snippet of the code when "p" is pressed to pause the game, and then after that occurs, be able to press "p" again and execute the other snippet.

My second question is...why when I trace the frame rate does it say it is 0.01 instead of 0? Found this sort of interesting....Anyways, here's the code. Tell me if you need more context, but I don't think you will.

        if (stage.frameRate == 30)
        {
            if (keyboardEvent.keyCode == Keyboard.P)
            {
                dispatchEvent(new NavigationEvent(NavigationEvent.PAUSEGAME));
                stage.frameRate = 0;
                checkIfPaused = true;
                trace("pause game");
                trace(stage.frameRate);
            }
        }
        if (stage.frameRate == 0.01)
        {
            if (keyboardEvent.keyCode == Keyboard.P)
            {
                stage.frameRate = 30;
                dispatchEvent(new NavigationEvent(NavigationEvent.RESUMEGAME));
                checkIfPaused = false;
                trace("resume game");
            }
        }

Solution

  • Nevermind, I figured it out. I just added else if instead of if for the second snippet and now it works =), but if anyone can answer my question about the frame rate being 0.01 in a nice fashion I'll give the answer to you