views:

296

answers:

2

In cocos2d, I'm trying to call a method on the Parent of a CocosNode. The app works fine, but I get an 'Object' may not respond to 'method' warning. The parent is a subclassed Cocos2d layer, so I'm guessing I need to cast parent somehow, but that generates fatal errors.

The method is like this

if(CGRectContainsPoint([newBrick boundingBox], touchedStartPoint)){
 [parent showChooser]; 
 return kEventHandled;
}

I've tried adding the following, but with no success...

if(CGRectContainsPoint([newBrick boundingBox], touchedStartPoint)){
 if([parent respondsToSelector:@selector(showChooser)]){
  [parent showChooser];
 }
 return kEventHandled;
}

Any ideas?

+1  A: 

Assuming that showChooser is a method defined on your subclass, you should just be able to write:

if(CGRectContainsPoint([newBrick boundingBox], touchedStartPoint)){
    [(YourLayerSubclass*)parent showChooser];
    return kEventHandled;
}

or, if you want to be a little safer:

if(CGRectContainsPoint([newBrick boundingBox], touchedStartPoint)){
    if( [parent isKindOfClass:[YourLayerSubclass class]] ) {
        YourLayerSubclass *subclassParent = (YourLayerSubclass*)parent;
        [subclassParent showChooser];
        return kEventHandled;
    }
}
Sixten Otto
Excellent. Thanks. If I may, why is the second option safer?
gargantaun
Because you're explicitly verifying that `parent` is an instance of the class you expect it to be, rather than just assuming it is.
Sixten Otto
... which, if the assumption is wrong, could cause a crash. To end Otto's sentence. ;)
GamingHorror
A: 

Hmmm...I was getting the same problem and follower your suggestion.It did work.... the warning dissapears, But now I ended up getting "expected specifier-qualifier-list before'ParentClass' errors... any ideas why?

Will
that's a different question. but my guess is that you've not included the header for the parent class.
gargantaun