azamsharp
azamsharp

Reputation: 20066

dealloc not fired when replacing scenes in Cocos2d

For some reason the dealloc of my CCLayer is not fired when replacing the scene. Here is the code to replace the scene:

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

The above code is triggered when I press a button.

I have placed a NSLog inside the dealloc method which is never triggered.

UPDATE 1:

I ended up solving the problem by manually releasing the memory just before replacing the scene.

Upvotes: 5

Views: 2154

Answers (2)

Lukman
Lukman

Reputation: 19164

According to this post at cocos2d-iphone forum, you just need to call self.isTouchEnabled = YES; in the init method to enable Cocos2D's to automatically call removeDelegate: method on the CCTouchDispatcher. Basically the following code should suffice:

- (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]; 
}

No need for onEnter and onExit as they should be handled automatically if you use self.isTouchEnabled = YES;

Upvotes: 0

gixdev
gixdev

Reputation: 570

When I first time begun to use cocos2d I had encountered this exactly same problem. In my case I'm added as targeted delegate the self and that means that reference count to the self was increased.

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

And I solved this by removing all delegates(also you can specify particular delegate) :

[[CCTouchDispatcher sharedDispatcher] removeAllDelegates];

Upvotes: 6

Related Questions