Pergunta

We are creating an SDK that is used in Cocos2d-x games (on Android/iOS).

As part of development, we have setup a test framework for running various tests on a real device (Android only at the moment).

One specific test needs to kill the app and launch it again.

It seems that Cocos2d-x does not provide any API for exiting (or killing) the running app.

Some online resources suggested that people have used the call to exit(0).

Is there any way to exit the game from Cocos2d-x ?

Calling exit(0) seems to crash:

#00  pc 0001e824  /system/lib/libc.so
#01  pc 0001c76c  /system/lib/libc.so (abort+4)
#02  pc 000122ed  /system/lib/libc.so
#03  pc 00010f9d  /system/lib/libc.so (dlfree+1184)
#04  pc 0000dabf  /system/lib/libc.so (free+10)
#05  pc 000cbf59  /data/app-lib/com.test.cocos2dx_testapp-1/libhellocpp.so (cocos2d::BitmapDC::~BitmapDC()+10)
#06  pc 00024639  /system/lib/libc.so (__cxa_finalize+156)
#07  pc 000248f1  /system/lib/libc.so (exit+6)
Foi útil?

Solução

The sample project HelloCpp has an exit button in its example. Look at HelloWorldScene.cpp and at the function call back menuCloseCallback (function might vary a bit based on version of cocos2dx you are on)

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
    CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}

For android the answer would be CCDirector::sharedDirector()->end();

Note that I don't think Apple will approve you if your app calls exit(0).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top