Take the 2-minute tour ×

I have a class extended from CCSprite that implements CCTargetedTouchDeledate like so:

@interface PianoKey : CCSprite <CCTargetedTouchDelegate> {


This has the following methods relating to the CCTouchDispatcher:

-(void) onEnter {
    [super onEnter];
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:YES];

-(void) dealloc {
    [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
    [super dealloc];

And also has the standard methods CCTouchesBegan etc. The idea is to simulate multi-touch by having each piano registered with the touch dispatcher.

This all works fine, except for when I change to a new scene. The touches for these piano keys are still being registered and will take priority over things like menu items etc in the new scene. So it appears the sprites are not being removed from the CCTouchDispatcher...

Any help is greatly recieved!

share|improve this question
Would I be right in thinking that I could create a class the inherits from CCLayer and contains a CCSprite, the layer matching the sprites dimensions? that might be easier... –  Alex Nov 24 '11 at 14:57
add comment

1 Answer

up vote 3 down vote accepted

if i recall correctly the [CCTouchDispatcher sharedDispatcher] retains its delegate, so your dealloc is never called. you have to call [[CCTouchDispatcher sharedDispatcher] removeDelegate:self] elsewhere, doing so your sprite will be deallocated correctly.

usually delegates are defined as assign, this unusual behaviour should be better documented

share|improve this answer
-(void) cleanup {} is the method where such retained delegates should be removed. Agree that this should be documented, normally delegates are not retained but here they are because they're added to an NSMutableArray. –  LearnCocos2D Nov 24 '11 at 17:25
Thanks both of you, -(void)cleanup{} works perfectly. –  Alex Nov 24 '11 at 17:43
add comment

Your Answer


