Game Development Reference
In-Depth Information
The Spider class is registered with CCTouchDispatcher to receive input as a
touch delegate, but this delegate association must also be removed on cleanup .
Otherwise, the scheduler or touch dispatcher would still keep a reference to the
Spider class even though it was released from memory, and that would likely cause a
crash shortly thereafter. And it must be in the cleanup method, not dealloc , for
the simple reason that the touch dispatcher holds (retains) a reference to your class. If
you don't instruct the touch dispatcher to remove your class, it will never be dealloc-
ated.
Listing 5-14. The Changed Spider Class
-(id) initWithParentNode:(CCNode*)parentNode
{
if ((self = [super init]))
{
...
// Manually add this class as receiver of targeted touch events.
[[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self
priority:-1
swallowsTouches:YES];
}
return self;
}
-(void) cleanup
{
// Must manually remove this class as touch input receiver!
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
[super cleanup];
}
// Extract common logic into a separate method accepting parameters.
-(void) moveAway:(float)duration position:(CGPoint)moveTo
{
[spiderSprite stopAllActions];
 
Search Nedrilad ::




Custom Search