2013-04-30 90 views
0

我只是無法弄清楚這個保留週期,並希望如果有人能幫我發現它。Cocos2d代表保留週期

我有一個RootController對象,強烈指出RootView

@interface RootController : CCNode <TouchDelegate, GUIDelegate, ModelViewDelgate> 
... 
@property (nonatomic, weak) CCDirector *director; 
@property (nonatomic) RootView *view; 
... 
@end 

@implementation 
- (id)init { 
    ... 
    _view = [[RootView alloc] initWithController:self]; 
    [self addChild:_view]; 
    ... 
    } 
    return self; 
} 
@end 

我有一個RootView對象保持到所述控制器的參考,以及一個activeGame,使我的遊戲類型之間切換,而無需知道其他的細節比它符合一<TouchDelegate>協議:

@interface RootView : CCScene 
... 
@property (nonatomic, assign) RootController *controller; 
@property (nonatomic) GameplayLayer <ModelViewDelgate> *activeGame; 
... 
@end 

@implementation RootView 
- (id)init { 
    ... 
    self.activeGame = [[GameplayLayer alloc] initWithDelegate:_controller root:self type:type]; 
    [self addChild:self.activeGame]; 
    ... 
} 
    return self; 
} 
@end 

最後,我有GameplayLayer,這是必要的,當RootView調用,應該由RootView被釋放:

@interface GameplayLayer : CCLayer <ModelViewDelgate, Updatable> 
... 
@property (nonatomic, assign) RootView *rootView; 
@property (nonatomic, assign) RootController <TouchDelegate> *touchDelegate; 
... 
@end 

當一個控制器類決定清理遊戲的時候(通常是遊戲的硬重置),我的項目中的其他任何類都會被釋放,除了這個GameplayLayer,它永遠不會接收到dealloc方法。我錯過了什麼?下面是我「重新啓動」我的遊戲...

[[CCDirector sharedDirector] replaceScene:[RootController node]]; 
+0

你在'RootView'的'dealloc'中發佈'GameplayLayer'嗎? – 2013-04-30 00:39:05

+0

我將'_activeGame.touchDelegate','_activeGame.rootView'和'_activeGame'設置爲nil。 – Clev3r 2013-04-30 00:47:21

+0

「self.activeGame = nil;'或'[_activeGame release];'。否則,你只是在指責,而不是破壞對象。或者(最佳答案),轉換爲ARC。 – 2013-04-30 00:48:52

回答

0

多虧了@JoshCaswell和@ LearnCocos2D一些發人深省的提問,我們能夠解決這個問題。事實證明,確實沒有問題。根據Cocos2d的要求,在onExit方法中,您必須告訴CCDirector刪除先前分配給該CCNode的所有觸摸代表。但是,如果您重寫該方法,則必須在退出該方法之前調用[super onExit],否則Cocos2d將無法刪除子項,因此某些元素將不會釋放。