cocos2d 2.0-rc2:结束director并重新启动 [英] cocos2d 2.0-rc2: end the director and restart

查看:176
本文介绍了cocos2d 2.0-rc2:结束director并重新启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个cocos2d动力游戏,使用UIKit menues,所以我只使用一个viewcontroller框架,这是游戏本身。此外,它只有一个场景。因为cocos2d 2.0的导演本身是一个 UIViewController 子类,所以我只是推它在我的 MenuViewController 当用户点击开始按钮:

I have a cocos2d powered game that uses UIKit menues, so I only use the framework for one viewcontroller, which is the game itself. Also, it only has one scene. Since cocos2d 2.0 the director itself is a UIViewController subclass, so I just push it in my MenuViewController when the user taps a start button:

-(void)startGameButtonPressed {

    CCDirectorIOS* director = (CCDirectorIOS *) [CCDirector sharedDirector];


    // Create an CCGLView with a RGB565 color buffer, and a depth buffer of 0-bits
    self.glView = [CCGLView viewWithFrame:CGRectMake(0, 0, 480, 320)
                              pixelFormat:kEAGLColorFormatRGB565    //kEAGLColorFormatRGBA8
                              depthFormat:0 //GL_DEPTH_COMPONENT24_OES
                       preserveBackbuffer:NO
                               sharegroup:nil
                            multiSampling:NO
                          numberOfSamples:0];

    // attach the openglView to the director
    [director setView:glView];
    [director runWithScene:[GameLayer scene]];
    [director setDelegate:(id <CCDirectorDelegate>) [[UIApplication sharedApplication] delegate]];
    [self.navigationController pushViewController:director animated:YES];
}

这在第一次调用方法时第一场比赛。当游戏结束时,我打电话 [[CCDirector sharedDirector] end]

This works fine for the first time the method is called, when the user starts the first game. When the game is over, I call [[CCDirector sharedDirector] end].

在appDelegate中完成(从默认的Cocos2d模板中改变)。我只把 CCGLView 作为保留属性放入我的 MenuViewController ,否则应用程序崩溃时 [[CCDirector sharedDirector] end] ,并且不保留 CCGLView 。我认为可能是一个cocos2d错误。在 [[CCDirector sharedDirector] end] 框架调用 [self setView:nil] ,但它仍然尝试访问(可能在另一个线程)。

Most of the director setup is done in the appDelegate (it's taken unchanged from the default Cocos2d template). I only put the CCGLView as a retained property into my MenuViewController, because otherwise the app crashes when [[CCDirector sharedDirector] end] is called and the CCGLView is not retained. I think that might be a cocos2d bug. In [[CCDirector sharedDirector] end] the framework calls [self setView:nil], but it still tries to access the view later on (probably on another thread).

现在的问题是,在我的方法的第二次调用上面(当用户想从菜单), startGameButtonPressed ,导演被推送,但屏幕保持黑色。游戏运行和响应,我只是没有看到任何东西。

The problem now is that on the second call of my method above (when the user wants to start another game from the menu), startGameButtonPressed, the director gets pushed but the screen remains black. The game is running and responding, I just don't see anything. Can someone please help me out with that?

推荐答案

确定,我有同样的问题,我能够修复它 。

OK, I had the same problem and I was able to "fix it".

当您设置CCGLView和[director setView:]时,即使弹出控制器,场景仍然存在。发生的唯一的事情是,场景被停止。

When you set the CCGLView and [director setView:], even if you pop the controller the scene still exists. the only thing that happens is that the scene gets stopped.

所以为了让重新启动工作,你必须检查是否已经有一个运行的场景即使它已停止,而不是 runWithScene:,您使用 replaceScene:

So in order to have the "restart" working, you have to check if there is already a running scene (even if it's stopped, and instead of runWithScene: you use replaceScene:.

这是我的代码,所以你可以看到:

Here is my code so you can see:

- (void)setupCocos2D {
    CCGLView *glView = [CCGLView viewWithFrame:CGRectMake(0, 0, 320, 480)
                               pixelFormat:kEAGLColorFormatRGB565   //kEAGLColorFormatRGBA8
                               depthFormat:0    //GL_DEPTH_COMPONENT24_OES
                        preserveBackbuffer:NO
                                sharegroup:nil
                             multiSampling:NO
                           numberOfSamples:0];

// HERE YOU CHECK TO SEE IF THERE IS A SCENE RUNNING IN THE DIRECTOR ALREADY    
if(![director_ runningScene]){
    [director_ setView:glView]; // SET THE DIRECTOR VIEW
    if( ! [director_ enableRetinaDisplay:YES] ) // ENABLE RETINA
        CCLOG(@"Retina Display Not supported");

    [director_ runWithScene:[HelloWorldLayer scene]]; // RUN THE SCENE

} else {
    // THERE IS A SCENE, START SINCE IT WAS STOPPED AND REPLACE TO RESTART
    [director_ startAnimation];
    [director_ replaceScene:[HelloWorldLayer scene]];
}      


[director_ setDelegate:(id <CCDirectorDelegate>) [[UIApplication sharedApplication] delegate]];

// I DO NOT PUSH BECAUSE I ALREADY PUSHED TO THIS CONTROLLER, SO I ADD THE COCOS2D VIEW AS A SUBVIEW
[self.view addSubview:[director_ view]];

}

希望这段代码会帮助你,一天试图想出这一点。
它可能不是正确的方式,甚至不是最漂亮的方式,但它的工作:)

Hope this code will help you, because I spent a whole day trying to figure this out. It may not be the correct way or even the prettiest way, but it's working :)

EDIT:
另外,请不要说,如果你的POP COCOS2D场景,你不必 [[CCDirector sharedDirector] end] 因为动画将被停止时视图是dealloc /已删除。

Also, please not that if you POP the COCOS2D scene, you don't have to [[CCDirector sharedDirector] end] as the animation will be stopped when the view is dealloc/removed.

这篇关于cocos2d 2.0-rc2:结束director并重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆