2011-03-08 68 views
5

由於某種原因,我的CCLayer的dealloc在替換場景時未被觸發。以下是替換場景的代碼:在Cocos2d中替換場景時dealloc未被觸發

[[CCDirector sharedDirector] replaceScene:[CCFadeTransition transitionWithDuration:2.0f scene:[HelloWorld scene]]]; 

上述代碼在我按下按鈕時觸發。

我已經在dealloc方法中放置了一個NSLog,它永遠不會被觸發。

更新1:

我結束了手動釋放內存解決問題只是更換現場。

+0

他們應該被自動處理這聲音,這是由HelloWorld的場景將會替換現場沒有公佈。你如何實例化場景,它是如何具有CCLayer實例的? – 2011-03-08 23:39:11

+0

意味着有東西保留圖層.. – xuanweng 2011-03-09 02:42:47

+0

您是否將任何當前圖層的引用傳遞給任何其他對象? 像Layer = currentLayer? – 2011-03-09 03:40:40

回答

6

當我第一次開始使用cocos2d時,我遇到了完全相同的問題。 在我的情況下,我被添加爲有針對性的委託自我,這意味着對自我的引用計數增加。

[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:2]swallowsTouches:NO];

我解決了這個通過刪除所有代表(也可以指定特別代表):

[[CCTouchDispatcher sharedDispatcher] removeAllDelegates];

0

this post at cocos2d-iphone forum,你只需要在init方法調用self.isTouchEnabled = YES;到使Cocos2D能夠在CCTouchDispatcher上自動調用removeDelegate:方法。基本上,下面的代碼就足夠了:

- (void)init { 
    // do the usual [super init] stuff 


    self.isTouchEnabled = YES; // don't forget the self prefix! 

    return self; 
} 

- (void)registerWithTouchDispatcher { 
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:2 swallowsTouches:NO]; 
} 

無需onEnteronExit,如果你使用self.isTouchEnabled = YES;

+0

投票結果如何?我沒有回答這個問題嗎? – Lukman 2011-03-10 00:32:41

+0

您可能正在回答問題的一部分,但您沒有澄清原因。在我看來,問題是關於管理場景的記憶,而且我不是100%確定的,但聽起來好像你在說場景將自己添加爲觸控代表。因此,觸摸調度器正在抓住它。我遇到了與@azamsharp相同的問題,我更加好奇爲什麼手動設置「isTouchEnabled」會導致場景正確釋放。 – goatlinks 2011-11-03 20:31:25