so i was programming a game in Xcode 3.2.4 with cocos2d and i made a class named player. as of now it only has the one function that does nothing.
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface Player : CCSprite {
}
-(void)leftButtonPressed;
@end
#import "Player.h"
@implementation Player
- (id)init{
if((self=[super init])){
self = [CCSprite spriteWithFile:@"playerPlane.png"];
}
return self;
}
-(void)leftButtonPressed{
}
@end `
whenever i try to call leftButtonPressed from anywhere, the entire app crashes with this in the console
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CCSprite leftButtonPressed]: unrecognized selector sent to instance 0x6e4bb50'
i initialize the Player as an instance in another class by doing
Player * thePlayer = [[Player alloc]init];
please help! thanks!!
This causes the problem:
What are you doing? You initialise the newly created (the caller that calls
alloc/initcreated it)Playerobject. Theinitmethod may even have returned another instance than the one that is currently executing (some strange unique cocoa thing). And when that was successful then you crate a newCCSpriteobject and assign it toself.Selfis then returned to the caller.(If you would not ARC – I assume you do – then this for sure would create a memory leak);
But the caller expects to deal with a
Playerobject and later sends theleftButtonPressedmessage to it which theCCSpritecannot respond to.That is, what the error message tells you.
You may use instead:
or something like: