2011-01-27 49 views
2

如何在上一個之後重新啓動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 node]; 
    [s addChild: layer]; 
    [director runWithScene:s]; 
} 

}

PlayLayer.m我回去,從現場查看使用:

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

應用程序是顯示圖像(1280x960x32,ImageView的內部滾動視圖),當用戶按下按鈕時,他可以啓動cocos2d場景。用戶然後可以離開場景返回查看(.xib)並繼續瀏覽圖像,直到他找到一個新的場景開始場景。

我相信這不是一個好辦法離開現場,但一切我試過導致應用程序崩潰或我不斷收到這個粉絲網第二次我開始場景。我如何重新啓動它?

回答

0

好吧,發現從內封閉現場和行之有效的解決方案。

雖然開始場景我想補充通知觀察者在我的ViewController:

(...) 
[director runWithScene:s]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
     selector:@selector(mySceneEnd:) name:@"scene_ended" object: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]; 

工程真的很不錯,應用程序運行後關閉sce嗯,但這是一個古老的問題/答案,現在肯定有更好的方法來做到這一點。

2

我終於在更換現場能擺脫粉絲網的。我就是這麼做的。在添加場景時,我會檢查它是第一次運行還是後續運行。

MyTestAppDelegate * appDelegate = (MyTestAppDelegate *)[[UIApplication sharedApplication] delegate]; 
EAGLView *glView; 
//check if scene is running first time then create OpenGLView, add to director and run the scene 
if ([[CCDirector sharedDirector] runningScene] == NULL) { 
    if(! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink]) 
     [CCDirector setDirectorType:kCCDirectorTypeDefault]; 

    CCDirector *director = [CCDirector sharedDirector]; 
    glView = [EAGLView viewWithFrame:[appDelegate.window bounds] 
            pixelFormat:kEAGLColorFormatRGB565 // kEAGLColorFormatRGBA8 
            depthFormat:0      // GL_DEPTH_COMPONENT16_OES 
         ]; 
    [director setOpenGLView:glView];   
    [director setDeviceOrientation:kCCDeviceOrientationPortrait]; 
    [director setAnimationInterval:1.0/60]; 
    [director setDisplayFPS:YES]; 
    [appDelegate.window addSubview:glView];  

    [[CCDirector sharedDirector] runWithScene: [MyScene node]]; 
} 
//if scene is to be reloaded, add back the openGLView which was removed when removing the scene 
else {  
    [appDelegate.window addSubview:[[CCDirector sharedDirector] openGLView]]; 
    [[CCDirector sharedDirector] replaceScene:[MyScene node]]; 
} 

對於消除當前正在運行的場景:

[[CCDirector sharedDirector] replaceScene:[MySceneTwoLayer scene]]; 
[[CCDirector sharedDirector].openGLView removeFromSuperview]; 

添加MySceneTwoLayer是可選的。這是一個空洞的場景。我正在使用它來確保以前的場景已被正確刪除。我查了一下,之前的場景的dealloc被正確調用。

我希望它有幫助。

+0

我會盡快檢查一下吧,謝謝記住!我目前的解決方案有點混亂... – yosh 2011-03-09 10:29:30

1

我用故事板和返回其中載有另一種觀點認爲我CCGLView米原UI視圖時,當跨越simliar問題就來了。

//when leaving the scene containing the CCGLView. 
[CCDirector director] popScene]; 

然後我在viewDidLoad中注意到該視圖控制器,當重新實例BT的SEGUE,在那裏你可以再重新添加場景將通過這個。

// in viewDidLoad 
[CCDirector director] pushScene: theSceneIRemovedEtc]; 

Admititedly這mightnot工作的所有情況,但肯定沒有爲我..

+0

工作就像一個魅力! – Myxtic 2014-08-19 13:29:21