2013-04-15 32 views
0

我想知道什么是正确的方式来重新启动cocos2d中的当前场景,因为我无法正常工作。我使用下面的代码来重新启动与实际场景,但只出现黑屏....重新启动cocos2d中的当前场景

CCScene *currentScene = [CCDirector sharedDirector].runningScene; 
CCScene *newScene = [[[currentScene class] alloc] init]; 

[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.7f scene:newScene]]; 

注:我不知道,如果这事做的事实(我想不会)该代码是从负责管理暂停菜单和内容的CClayer子类运行的。注:这是一个通用的重新开始,我假装它与每个场景一起工作,所以我 谢谢!

+0

检查内存泄漏或overreleasing对象(启用僵尸) – LearnCocos2D

+0

没有内存泄漏或overrelasing对象,从来就刚发现的问题:[currentScene类]在返回而不是自定义类的名称CCScene。这就是为什么我看到一个黑屏。 – Potajedehabichuelas

回答

2

对于其他人都遇到同样的问题,这是我找到克服问题的方式:

Class SceneClass = NSClassFromString(nameOfClass); 
[[CCDirector sharedDirector]replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[SceneClass scene]]]; 

哪里nameOfClass是包含名称的字符串实现场景重启的类。这种方式就像一个魅力,你甚至不必#import .h文件。

1

我想这与手动分配有关。在大多数情况下,最好调用cocos2d静态构造函数,并让它为您处理内存管理。这是我做的.-

MyClassScene *newScene = [MyClassScene node]; 
[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:kTransitionTime scene:newScene withColor:ccc3(255, 255, 255)]]; 

希望它有帮助。

+0

谢谢,但我得到完全相同的结果。这里的事情是,这是一个通用的重新开始,我假装它与每个场景一起工作,所以这就是为什么我调用CCScene * currentScene = [CCDirector sharedDirector] .runningScene;要知道现在哪个场景正在运行 – Potajedehabichuelas

+0

我明白了。控制台中是否有错误? – ssantos

+0

不,完全没有! – Potajedehabichuelas

0

试试这个:

CCScene *currentScene = [[CCDirector sharedDirector] runningScene]; 

[[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.7f scene:[currentScene node]]]; 
+0

[CCScene节点]产生一个警告,然后应用程序崩溃: 原因:' - [CCScene节点]:无法识别的选择器 – Potajedehabichuelas