第二次输入游戏时,Cocos2D-JS游戏iOS崩溃



我们正在使用Cocos2D-JS开发可以启动不同游戏的iOS应用程序。因此,我在本机应用程序ViewController中添加一个按钮,然后单击按钮开始游戏,就像这样:

-(void)didClickGame2Btn:(id)sender
{
    //加载游戏
    cocos2d::Application *app = cocos2d::Application::getInstance();
    // Initialize the GLView attributes
    app->initGLContextAttrs();
    cocos2d::GLViewImpl::convertAttrs();
    // Use RootViewController to manage CCEAGLView
    RootViewController *rootViewController = [[RootViewController alloc] init];
    rootViewController.wantsFullScreenLayout = YES;
    [self.navigationController presentViewController:rootViewController animated:YES completion:^{
    // IMPORTANT: Setting the GLView should be done after creating the RootViewController
    cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView((__bridge void *)rootViewController.view);
    cocos2d::Director::getInstance()->setOpenGLView(glview);
    NSString *documentDir = [SEGetDirectories dirDoc];
    NSString *wPath = [NSString stringWithFormat:@"%@/GameData/Game2",documentDir];
    NSLog(@"document------:%@",documentDir);
    std::vector<std::string> searchPathList;
    searchPathList.push_back([wPath UTF8String]);        
    cocos2d::FileUtils::getInstance()->setSearchPaths(searchPathList);
    //run the cocos2d-x game scene
    app->run();
}];
    [[UIApplication sharedApplication] setStatusBarHidden:true];
}

rootviewController包含游戏视图。然后,我们在游戏中添加一个按钮,该按钮用于退出游戏。单击"退出游戏"按钮喜欢的事件代码:

//exit the game and close the view controller
gameEndCallBack:function(sender){
    cc.log("director end............");
    cc.director.end();
    var ojb = jsb.reflection.callStaticMethod("ViewControllerUtils", "dismissCurrentVC");
}

我们使用反射来解散rootviewController:

+(void)dismissCurrentVC
{
    UIViewController *currentVC = [ViewControllerUtils getCurrentVC]; //这里获取最顶层的viewcontroller
    [currentVC dismissViewControllerAnimated:YES completion:^{
        NSLog(@"xxx");
    }];
}

第一次进入游戏时,一切都还可以,但是在解散了rootviewController之后,我们尝试再次输入游戏,它崩溃了。

崩溃线在sciptingcore :: runscript metod并执行代码:

evaluatedOK = JS_ExecuteScript(cx, global, *script, &rval);

崩溃信息为" exc_bad_access"。

这与本主题大致相同,但是其中的方法并不能解决问题。http://discuss.cocos2d-x.org/t/how-to-destroy-a-cocos-game-game-on-ios-completely/23805

这个问题使我感到困惑,我对此没有解决方案。谁能给我一些帮助?

您可以在应用程序中使用该应用程序来支持多个游戏。您所做的一切都是必需的,但是除此之外,请按照以下说明进行操作。

首先,可可提供了cocos2d::Application的单例实例,该实例无法再次重新启动,尤其是在iOS中。因此,结束董事cc.director.end();的方法无法帮助您。

您应仅使用函数调用cocos2d::Application::getInstance()->run();启动一次应用程序,并且下次如果要启动游戏层,则不应再次调用此方法。

相反,只需暂停cocos2d::Director::getInstance()->pause();,然后在您想停止游戏时恢复cocos2d::Director::getInstance()->resume();

在这种方法中,如果您解散/dealloc the View-controller实例,则您应该再次创建GLVIEW cocos2d::GLView实例而无需调用运行方法。

另一个问题是,请注意加载新场景的延迟。Glview将展示以前的游戏场景一段时间。在新场景准备就绪时,要进行一项工作将显示空白屏幕。

希望这对您有帮助。

最新更新