Alex
Alex

Reputation: 1322

Remove touch from CCSprite with CCTouchDispatcher

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!

Upvotes: 1

Views: 1029

Answers (1)

Ultrakorne
Ultrakorne

Reputation: 1303

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

Upvotes: 3

Related Questions