Cocos2d-x is a suite of open-source, cross-platform, game-development tools utilized by millions of developers across the globe. Its core has evolved to serve as the foundation for Cocos Creator 1.x & 2.x.
// 对于Android应用来说,单次应用生命周期内,如果多次启动cocos,则会由于初次resume时,surface尚未创建,会导致firstTime变量为true,
// 在第二次启动cocos时会导致无法调用applicationWillEnterForeground函数导致单例Direct实例中_invalid无法重置为false
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnResume() {
static bool firstTime = true;
if (Director::getInstance()->getOpenGLView()) {
// don't invoke at first to keep the same logic as iOS
// can refer to https://github.com/cocos2d/cocos2d-x/issues/14206
if (!firstTime)
Application::getInstance()->applicationWillEnterForeground();
cocos2d::EventCustom foregroundEvent(EVENT_COME_TO_FOREGROUND);
cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&foregroundEvent);
firstTime = false;
}
}
Steps to Reproduce:
在启动Cocos2dxActivity时由于GLSurfaceView异步初始化,会导致activity的onResume先于GLSurfaceView的onSurfaceCreated执行,进而导致Cocos2dxRenderer的nativeInit函数会在nativeResume之后执行,就会导致Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnResume中由于Director单例中尚未setOpenGLView,使得firstTime静态变量无法设置为false
最终现象会是,在初次启动Cocos2dxActivity然后退出,再重新进入,会出现无法正常渲染动画 代码如下