Cocos2d触摸调度程序导致对象保留

Maj*_*ter 4 touch dealloc cocos2d-iphone ios

我有cocos2d的问题.我做了一个接受触摸的课程.类的子类CCLayerinit看起来像这样:

- (id)initWithFrame:(CGRect)frameSize
{
    self = [super init];
    if (self)
    {
        frame = frameSize;
        size = frame.size;
        origin = frame.origin;
        [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

所以一切都很简单.frame,size并且origin是类变量,但这现在无关紧要.所以我注册了我的班级女巫touchDispatcher,让我可以处理.触摸处理完成如下:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    return YES;
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    //Some touch logic which i need.
}
Run Code Online (Sandbox Code Playgroud)

dealloc我发布所有保留的信息并取消注册touchDispatcher.但从dealloc未被称为.如果我不与注册touchDispatcherdealloc正确调用.如果重要的话,这个类作为子类添加到另一个CCLayer子类中,并且在该类中dealloc我释放了这个类.

我错过了什么?

Yve*_*org 7

澄清giorashc的答案,做到这一点.:

- (void)onEnter {
    [super onEnter];
    [[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

- (void)onExit {
    // called before the object is removed from its parent
    // force the director to 'flush' its hard reference to self
    // therefore self's retain count will be 0 and dealloc will
    // be called.
    [super onExit];
    [[CCDirector sharedDirector].touchDispatcher removeDelegate:self];
}
Run Code Online (Sandbox Code Playgroud)