2013-04-30 98 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将无法删除子项,因此某些元素将不会释放。