cocos2d-x 游戏进入后台崩溃 [英] cocos2d-x game crashes when entered background
问题描述
我的 cocos2d-x 游戏在进入后台时崩溃.这是来自 AppDelegate 的一些代码:
my cocos2d-x game crashes when entering background. here is some code from AppDelegate:
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{
CCDirector::sharedDirector()->pause();
CCUserDefault::sharedUserDefault()->flush();
CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
CCDirector::sharedDirector()->resume();
CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
和错误信息:
libGPUSupportMercury.dylib`gpus_ReturnNotPermittedKillClient:
0x3797e094: trap
0x3797e096: nop
请注意,它总是在 iPhone 上崩溃,但 99% 在 Android 上崩溃(好吧,当游戏没有加载大图像等时)
note that it always crashes for iPhone, but 99% crashes on Android (okay when the game haven't load large images etc)
我已经尝试过 CCDirector::sharedDirector()->stopAnimation() 并且它适用于 iOS.但 Android 仍然崩溃(不是立即崩溃.当返回应用程序时,屏幕变黑(但我认为它仍在运行,因为背景音乐仍在播放.然后大约 5 秒后崩溃)
I've tried CCDirector::sharedDirector()->stopAnimation() and it works great for iOS. But still crashes for Android (not immediately. when returning back to the app, the screen become black (but i think it is still running because background music is still playing. then about 5 seconds later it crashes)
编辑 2:Eclipse中的错误信息:
EDIT 2: The error message in Eclipse:
libEGL call to OpenGL ES API with no current context (logged once per thread) (red warning text)
libc Fatal signal 11 (SIGSEGV) at 0x5f012000 (code=2) (black text)
推荐答案
应用程序委托方法 applicationDidEnterBackground:
在您的应用程序转换到后台之后调用 ,但是 之前您的申请被暂停.不幸的是,您不能在后台执行 任何 GPU 指令,否则看门狗会终止您(如您在此处看到的).
The app delegate method applicationDidEnterBackground:
is called after your application transitions to the background, but before your application is suspended. Unfortunately, you may not perform any GPU instructions while in the background, or the watchdog will terminate you (as you're seeing here).
假设您的 CCDirector::sharedDirector()->pause()
调用负责停止您的图形/动画循环,您应该将其移至 applicationWillResignActive:
委托方法.该方法在您的应用程序转换到后台之前调用.
Assuming that your CCDirector::sharedDirector()->pause()
call is responsible for stoping your graphics/animation loop, you should move that to the applicationWillResignActive:
delegate method. That method is called before your application transitions to the background.
无论您的代码结构如何,请确保在您从 applicationWillResignActive:
委托调用返回之前完全刷新并停止动画循环.
However you have your code structured, make sure your animation loop is completely flushed and stopped before you return from the applicationWillResignActive:
delegate call.
注意:这个答案是关于为什么它总是在 iOS 上崩溃
这篇关于cocos2d-x 游戏进入后台崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!