Dealloc does not start when replacing scenes in Cocos2d

For some reason, the dealloc of my CCLayer does not start when replacing a scene. Here is the code to replace the scene:

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

The above code runs when I click the button.

I placed the NSLog inside the dealloc method, which never starts.

UPDATE 1:

I decided to solve the problem by manually freeing the memory before replacing the scene.

+5
source share
2 answers

When I first started using cocos2d, I encountered this exact same problem. In my case, I was added as a target delegate for myself, which means that the counter of references to the "I" has been increased.

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

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

[[CCTouchDispatcher sharedDispatcher] removeAllDelegates];

+6
source

cocos2d-iphone, self.isTouchEnabled = YES; init, Cocos2D removeDelegate: CCTouchDispatcher. :

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

onEnter onExit, , self.isTouchEnabled = YES;

0

All Articles