最近遇到一个cocos2dx 骨骼动画注册事件播放音效,在骨骼动画播放的时候,按HOME键退到桌面,再次打开游戏的时候,会黑屏。
解决办法如下,可能不是太完美,至少解决了大部分问题。
1.在org.cocos2dx.lib下的 Cocos2dxRenderer.java 中添加native方法
public static native void setIsPause(boolean isPause);
2.在Cocos2dxRenderer.java对应的Java_org_cocos2dx_lib_Cocos2dxRenderer.cpp中添加对应的方法
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_setIsPause(JNIEnv* env, jobject thiz, jboolean ispause) {
CCDirector::sharedDirector()->setAndroidPause(ispause);
}
3.在CCDirector.cpp中添加setAndroidPause用来标记是否已经暂停
//设置暂停状态
void CCDirector::setAndroidPause(bool isPause)
{
m_bAndroidPaused = isPause;
}
//获取暂停状态
bool CCDirector::getAndroidPause()
{
return m_bAndroidPaused;
}
4.在org.cocos2dx.lib下的Cocos2dxActivity.java中的onPause添加 1 步骤的方法
@Override
protected void onPause() {
Cocos2dxRenderer.setIsPause(true);
super.onPause(); Cocos2dxHelper.onPause();
this.mGLSurfaceView.onPause();
}
5.在CCTextureCache.cpp的 VolatileTexture::reloadAllTextures() 方法中添加一行代码,用来恢复暂停状态
void VolatileTexture::reloadAllTextures()
{
isReloading = true; CCLOG("reload all texture");
... ...//此处省略中间内容
isReloading = false;
//最后一行将暂停设置为false
CCDirector::sharedDirector()->setAndroidPause(false);
}
6.播放音乐时候判断一下是否是暂停状态还没有恢复过来 SimpleAudioEngine.cpp中
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop)
{
if(CCDirector::sharedDirector()->getAndroidPause())
return ;
std::string fullPath = getFullPathWithoutAssetsPrefix(pszFilePath);
return playEffectJNI(fullPath.c_str(), bLoop);
}