This project is read-only.

Pause and Resume Game

Topics: Windows Phone 8
Sep 3, 2013 at 10:26 AM
Edited Sep 3, 2013 at 10:40 AM
Hi,

I am trying to make a game, I have a problem with Pause and resume my game.
I used newest Github source available.
I tested your TestProject in Windows phone to learn samples. But when I pressed "Home" to hide App and display Home screen, then I touch long press Back to chose recent App, resume it to work, but all sprite, label, menu item, and other items was disappear and black screen was displayed.

Can you tell me about this problem and solution for it?
Thanks so much!

Daniel
Sep 4, 2013 at 12:12 AM
I'll check it out to see what's the problem.
Sep 4, 2013 at 12:13 AM
I tried it on the WP7 emulator and it worked. Are you on Windows Phone 8?
Sep 4, 2013 at 3:13 AM
Edited Sep 4, 2013 at 3:49 AM
I tried on WP7 with Angry Ninjas Sample, it worked with no problem. But on Lumia 520 WP8, I tested this TestProject from Github Source, it worked fluently, but Pause/Resume is a problem. Please check it on WP8. I also tried a new project and it was same!

P/s: I 've just debugged.

Angry Ninjas :
Press Home -> Call ApplicationDidEnterBackground() -> Call Pause()
Long Press Back -> Call ApplicationWillEnterForeground() -> Call Resume()
WP8 Test Project (Sample):
Press Home -> Call ApplicationDidEnterBackground() -> Call Pause()
Long Press Back -> Not Call ApplicationWillEnterForeground() ->Not Call Resume()
Thanks!
Sep 4, 2013 at 5:56 PM
ah ha. That looks like a MonoGame issue.
Sep 5, 2013 at 5:39 AM
Edited Sep 5, 2013 at 5:44 AM
No, I think it is not a MonoGame issue, because I've tested the lastest MonoGame source
MonoGame. It work perfectly with Pause and Resume game!

Please consider about this issue ..
Sep 5, 2013 at 5:46 AM
Edited Sep 5, 2013 at 5:47 AM
if ApplicationWillEnterForeground is not called, then it is MonoGame. We simply hookup the events on the game object and wait for the callback. it would see that the event in MonoGame for WP8 is not firing properly or reliably. That's not surprising to me since the event model for the game lifecycle in MonoGame is the one place where it suffers most notably.

Evidence of this is in CCApplication:
        game.Activated += GameActivated;
        game.Deactivated += GameDeactivated;
        game.Exiting += GameExiting;

    private void GameActivated(object sender, EventArgs e)
    {
        // Clear out the prior gamepad state because we don't want it anymore.
        m_PriorGamePadState.Clear();
        CocosDenshion.CCSimpleAudioEngine.SharedEngine.SaveMediaState();
        ApplicationWillEnterForeground();
    }

    private void GameDeactivated(object sender, EventArgs e)
    {
        ApplicationDidEnterBackground();
        CocosDenshion.CCSimpleAudioEngine.SharedEngine.RestoreMediaState();
    }
The other possibility is an exception occurring in SaveMediaState that is halting the GameActivated call:
        CocosDenshion.CCSimpleAudioEngine.SharedEngine.SaveMediaState();
Sep 5, 2013 at 5:50 AM
Sure enough, upon resume, the simple audio engine is throwing an exception.
Sep 5, 2013 at 5:58 AM
Confirmed this is a monoGame bug.

https://github.com/Cocos2DXNA/cocos2d-xna/issues/325
Sep 5, 2013 at 6:00 AM
Totallyevil, Thanks so much!
Sep 5, 2013 at 6:02 AM
Here is the bug report for MonoGame:

https://github.com/mono/MonoGame/issues/1971
Sep 5, 2013 at 6:12 AM
here's your fix:

https://github.com/Cocos2DXNA/cocos2d-xna/pull/326

This is a work around for the MonoGame bug.
Sep 6, 2013 at 6:12 AM
The MonoGame PR I put up does indeed fix this problem.