views:

30

answers:

1

I have class called "HelloWorld.m" and another class called "Person.m". I have declared Person instance in the HelloWorld.m class as follows:

Person *person;

Now, when the user touches the screen I fire the following method:

[person foo]; 

But I get the error saying invalid selector sent.

NOTE: Person class inherits from the CCSprite class. What am I doing wrong?

Person.h:

-(void) foo; 

Person.m:

-(void) foo 
{
    NSLog(@"foo called!");
}

UPDATE 1: In the HelloWorld.h and HelloWorld.m I have declarated person as a property. Here is the call to the person.foo method.

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    [self.person foo]; 
}

HelloWorld.h:

// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
#import "Person.h" 

// Importing Chipmunk headers
#import "chipmunk.h"

// HelloWorld Layer
@interface HelloWorld : CCLayer
{
    cpSpace *space;
    Person *person; 

}

// returns a Scene that contains the HelloWorld as the only child
+(id) scene;
-(void) step: (ccTime) dt;
-(void) addNewSpriteX:(float)x y:(float)y;

@property (nonatomic,retain) Person *person;


@end
+1  A: 

Did you import the Person header into the top of the HelloWorld file?

#import "Person.h"

Did you initialise the person pointer?

person = [[Person alloc] init];

Does the Person class have a foo method?

No one in particular
Yes, I imported Person.h. I initialized the person inside the init method and yes Person does have a foo method.
azamsharp
Seems like it is related to CCSprite. Since it says [CCSprite foo] unrecognized selector sent to ...
azamsharp
Could we see the code where you make the call to the person?
No one in particular
Updated the code!
azamsharp
Thanks. Could we see the @property definition for person?
No one in particular
@property is defined in the above code! Thanks! For some reason I think it looks for CCSprite (base class for Person) for the method foo instead of the Person class.
azamsharp
I'm going through the list of mistakes that I always do and I'm not seeing anything yet. I agree with it looks like the Person is coming across as a CCSprite but I can't see how.
No one in particular
I went ahead and remove the inheritance from CCSprite. Now I am using CCSprite as an instance inside the Person class and it is working fine. Maybe CCSprite behaves strange when inherited!
azamsharp
It's very strange. Your code looked good. Wish I knew why it behaved as it did. Still, good to hear that you got a working solution.
No one in particular