cocos2d / cocos2d-x

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.
https://www.cocos.com/en/cocos2d-x
18.04k stars 7.05k forks source link

Android Platform Cocos2dxRenderer 初始化时序问题 #20026

Open Gslullaby opened 4 years ago

Gslullaby commented 4 years ago

Steps to Reproduce:

在启动Cocos2dxActivity时由于GLSurfaceView异步初始化,会导致activity的onResume先于GLSurfaceView的onSurfaceCreated执行,进而导致Cocos2dxRenderer的nativeInit函数会在nativeResume之后执行,就会导致Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnResume中由于Director单例中尚未setOpenGLView,使得firstTime静态变量无法设置为false

最终现象会是,在初次启动Cocos2dxActivity然后退出,再重新进入,会出现无法正常渲染动画 代码如下

// 对于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;
        }
    }
shang-he commented 4 years ago

这个问题你们是怎么解决的

WanDa1993 commented 3 years ago

同求,因为第一次创建Cocos2dxActivity是没问题的,但是第二次创建时,就会直接黑屏,然后Crash

WanDa1993 commented 3 years ago

@Gslullaby 老哥能分享下,后面你解决方案么