Hi I recently updated to COCOS2D v3 and the code I used to use to subclass a CCSprite no longer works and I am getting an error when I try to assign values to my properties.
@interface Alien : CCSprite
{
int _minMoveDuration;
int _maxMoveDuration;
int _hp;
}
@property (nonatomic, assign) int hp;
@property (nonatomic, assign) int minMoveDuration;
@property (nonatomic, assign) int maxMoveDuration;
@end
@interface SmallFastAlien: Alien
{
}
+(id)alien;
@end
Inside the main file I then set my properties for the alien class:
#import "Alien.h"
@implementation Alien
@synthesize hp = _hp;
@synthesize minMoveDuration = _minMoveDuration;
@synthesize maxMoveDuration = _maxMoveDuration;
@end
@implementation SmallFastAlien
+ (id)alien
{
SmallFastAlien *alien = nil;
/*if ((alien = [[[super alloc] initWithSpriteFrameName:@"small-1.png"] autorelease]))
{
}*/
alien = [CCSprite spriteWithImageNamed:@"small-1.png"];
alien.hp = 1;
//alien.minMoveDuration = 3;
//alien.maxMoveDuration = 5;
return alien;
}
@end
However, I then get an error when the hp property is set. This did work in v2, but no longer works in v3 and gives the following error:
-[CCSprite setHp:]: unrecognized selector sent to instance 0x10a4c5cb0
Any advice would be greatly appreciated.
You create a CCSprite, that's why:
alien = [CCSprite spriteWithImageNamed:@"small-1.png"];
You ought to be creating an instance of your class, for instance:
alien = [SmallFastAlien spriteWithImageNamed:@"small-1.png"];
PS: ivar declaration in @interface
and @synthesize
in @implementation
are unnecessary, so is the assign
keyword (specifically for int and other primitive data types). Xcode auto-synthesizes ivars (with underscore prefix) and getter/setter, assign only affects object (id
) types. You can shorten your code to:
@interface Alien : CCSprite
@property (nonatomic) int hp;
@property (nonatomic) int minMoveDuration;
@property (nonatomic) int maxMoveDuration;
@end
@implementation Alien
@end