适当的cocos2d场景重启? [英] Proper cocos2d scene restart?

查看:234
本文介绍了适当的cocos2d场景重启?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在之前重新启动cocos2d场景和清理内存?



[director replaceScene:s ] 正在显示粉红色屏幕。不能让它工作。



我所做的解决方法如下,但是在关闭场景之后,这种方式应用程序运行速度非常慢,并且慢慢对触摸做出反应。 MainViewController.m

   - (IBAction)startScene {

[ CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];

if([director runningScene]){
[director end];
works = NO;
}
if(!works){
EAGLView * glview = [EAGLView viewWithFrame:CGRectMake(0,0,768,1024)
[self.view addSubview:glview];
[director setOpenGLView:glview];
CCLayer * layer = [PlayLayer node];
CCScene * s = [CCScene节点];
[s addChild:layer];
[director runWithScene:s];
}

}



PlayLayer.m 中,我使用以下方式从场景返回:

  CCDirector * d = [CCDirector sharedDirector]; 
EAGLView * v = [d openGLView];
[v removeFromSuperview];

应用程序正在显示图像(1280x960x32,scrollview中的imageview),当用户按下按钮时,他可以启动cocos2d现场。然后,用户可以从场景返回到查看(.xib)并继续浏览图像,直到他找到一个新的开始场景。



我相信这不是一个好的方式离开现场,但一切我试图导致应用程序崩溃或我不断得到这个粉红色的屏幕第二次我开始现场。如何重新启动它?

解决方案



在启动场景时,我在viewcontroller中添加通知观察器:

  。)
[director runWithScene:s];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mySceneEnd :) name:@scene_endedobject:nil];

已连接到:

   - (void)mySceneEnd:(NSNotification *)notif {
if([director runningScene])
[director end]
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

然后在场景结束方法中发送通知到我的viewcontroller:

  EAGLView * v = [[CCDirector sharedDirector] OpenGLView]; 
[v removeFromSuperview];
[[NSNotificationCenter defaultCenter] postNotificationName:@scene_ended
object:nil userInfo:nil];

工作真的很好,应用程序运行后关闭场景快,但它是一个老问题/答案,肯定是一个更好的方法来做到现在。


How do I restart cocos2d scene and clean memory after the previous one?

[director replaceScene:s] is displaying pink screen. Can't get it to work.

The work-around I made is below, however after closing scene this way app runs very slow and slowly reacts to touch. MainViewController.m:

- (IBAction)startScene { 

[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];

if ([director runningScene]) {
    [director end];
    works = NO;
}
if (!works) {
    EAGLView *glview = [EAGLView viewWithFrame:CGRectMake(0,0, 768, 1024)]; 
    [self.view addSubview:glview];
    [director setOpenGLView:glview];
    CCLayer *layer = [PlayLayer node];
    CCScene *s = [CCScene node];
    [s addChild: layer];
    [director runWithScene:s];
}

}

Inside PlayLayer.m I go back from scene to view using:

CCDirector *d = [CCDirector sharedDirector];
EAGLView *v = [d openGLView];
[v removeFromSuperview];

Application is displaying images (1280x960x32, imageview inside scrollview) and when user presses the button he can start cocos2d scene. User can then leave from scene back to view (.xib) and continue browsing through images till he finds a new one to start scene with.

I believe this is not a good way to leave scene but everything else I tried is causing app to crash or I keep getting this pink screen 2nd time I start scene. How do I restart it ?

解决方案

Ok, found a solution that closes the scene from inside and works well.

While starting scene I add the notification observer in my viewcontroller:

(...)
[director runWithScene:s];
[[NSNotificationCenter defaultCenter] addObserver:self 
      selector:@selector(mySceneEnd:) name:@"scene_ended" object:nil];

That is connected to:

- (void) mySceneEnd:(NSNotification *)notif {   
    if ([director runningScene])
        [director end];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

Then in the scene ending method I'm sending the notification to my viewcontroller:

EAGLView *v = [[CCDirector sharedDirector] openGLView];
[v removeFromSuperview];
[[NSNotificationCenter defaultCenter] postNotificationName:@"scene_ended" 
     object:nil userInfo:nil];

Works really nice, app runs fast after closing scene, but it's an old question/answer and there is surely a better way to do it now.

这篇关于适当的cocos2d场景重启?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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