Proper way to close / exit game?

Hi,

Would like to know, what ways developers implement in closing / exit their game? Right now, I’m testing with Android ( Kindle / Nexus 7 ) and have implemented the following inside a node:

// use this for initialization
onLoad: function () {
this.KeyBoardExit();
},
KeyBoardExit:function(){
var self = this;
cc.EventListener.create({
event:cc.EventListener.KEYBOARD,
onKeyPressed:function(keyCode, event){
// if(keyCode == cc.KEY.back){
//cc.game.end();
//}}});

  **switch(keyCode)**{
                    case cc.KEY.**back**:
                    **cc.game.end();**
                   //cc.audioEngine.uncacheAll();
                  //  cc.audioEngine.stopAll();
                    break;
                    case cc.KEY.**home**:
                    **cc.game.end();**
                   // cc.audioEngine.uncacheAll();
                 //   cc.audioEngine.stopAll();
                    break;}    
 }}, self.node);</code>

However, the above doesn’t work…The thing is that when I exit the game using the buttons from the tablet ( home , backspace ) the game exits but the background music keeps playing. Would appreciate feedback concerning the above…Thanks and God Bless…

Sincerely,

Sunday

Hi,

I cannot test it on tablets yet, but my implementation is a bit different. I call the cc.audioEngle.stopAll() function before the cc.game.end() call. See the following sample code:

    cc.eventManager.addListener({
        event: cc.EventListener.KEYBOARD,
        onKeyPressed: function(keyCode, event) {
            if ((keyCode == cc.KEY.back) || (keyCode == cc.KEY.backspace))
                cc.game.end();
        }.bind(this)
    }, this.node);

Please test it, and share the result with us.

Best regards,
Zsolt

Hi @PZsolt27

Ok, will test it on my first game with Cocos Creator, I had a close button within my game, just in case I couldn’t find a solution, but now, I will test the solution you provided and see if it works…on both IOS and Android :grinning: God Bless…

Sincerely,

Sunday

Hi @luke2125,

since my last post I was able to build and test the Android version of my game and it seems that after the cc.game.end(); the game quits, but it crashes. I get the the following message:

Did you test your version?

Best reagards,
Zsolt

Hi @PZsolt27,

Will be testing this week on IOS and Android…:slight_smile: God Bless…

Sincerely,

Sunday

Hi @luke2125,

thanks for your reply. I’m still fighting with this exit bug, I don’t want to release my game with it. I created a minimal test app that works fine. So, I will add the features one-by-one to localize the problematic part. I hope it won’t take too much time.

Best regards,
Zsolt

Hi @PZsolt27

I tested the code in my game with the following:

Nexus 7: Android v 6.0.1 - Works.…hit back button and game ends, and no music…
Samsung Galaxy Note II : Android v4.3 - Works.…hit back button and game ends, and no music…
IOS Phone 5S - No back buttton…Just home…Works…

Maybe, it has something to do with your code? Also, the version I tested was for build / compile to default template, …I did test the IOS Binary though, build then compile within Xcode 8.2… I’m going to build and compile a binary for Android tomorrow and see…God Bless…

Sincerely,

Sunday

Hi @luke2125,

thanks for the test results. I’ll continue testing my project to find the problem. Meanwhile I started to code my second game, and I’m going to make an Android build today afternoon. I will check every single step when will it crash on exit.

Best regards,
Zsolt

Hi all,

based on my investigations, the following code does not work:

cc.Class({
    extends: cc.Component,

    properties: {
    },

    // use this for initialization
    onLoad: function () {
        this.node.runAction(cc.sequence(
                cc.delayTime(2), 
                cc.callFunc(this.onFinish, this)
            ));
    },

    onFinish: function (sender) {
        cc.game.end();
    },
});

It is just a minimal piece of code, the original code used fade-out before it closed the game. If I use a button to exit immediately, it works fine:

onTapExit: function (event) {
    cc.game.end();        
},

What is the problem with the first code-snippet? I think there is a problem with the action handling, but I also tried to use a counter to close the game a few frames later from the update() function.

Best regards,
Zsolt

Hi friends! I just want to share my experience.
On my project with Cocos Creator 1.9.3 “cc.game.end();” was crashing both android and win32
After I spend 1 days of testing/scratching I found “cc.director.end();” works perfect!
I hope this note helps someone like me :slight_smile:

2 Likes