cocos2d 重启_cocos2d 2.0-rc2:结束director并重新启动

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 ) [[UIApplication sharedApplication] delegate]];

[self.navigationController pushViewController:director animated:YES];

}

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].

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).

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".

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.

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 ) [[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:

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值