I have a class called Projectile (it inherits from the Cocos2D CCSprite Class, but I doubt that’s related to the problem). All it contains is an int called ‘type’, which I have made into a property.
Projectile.h:
@interface Projectile : CCSprite{
int type;
}
@property int type;
@end
Projectile.m:
#import "Projectile.h"
@implementation Projectile
@synthesize type;
@end
Now all I’m trying to do is set that property in the main class of my game, like so:
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
Projectile *newProjectile = [CCSprite spriteWithSpriteFrameName:@"Bullet.png"];
newProjectile.position = spaceShip.position;
newProjectile.type = kLaser; // defined earlier as #define kLaser 1
[objectsSpriteSheet addChild:newProjectile z:kProjectileZValue];
[projectiles addObject:newProjectile];
return YES;
}
Every time I run this, it crashes on the newProjectile.type = kLaser line:
-[CCSprite setType:]: unrecognized selector sent to instance 0x1ed82f10
It seems almost too simple to get wrong, and Xcode is clearly aware of the property as it autocompletes it and doesn’t give me an error to say it doesn’t exist.
I’ve also got very similar code elsewhere which executes perfectly.
If anyone could shed some light on this I would be most grateful, I’m sure I could get round it another way, but I’d like to understand why this doesn’t work if possible.
Well yes, when you are saying
Projectile *newProjectile = [CCSprite spriteWithSpriteFrameName:@"Bullet.png"];, newProjectile will be an object of type CCSprite, which doesn’t have that property.