Reputation: 594
I'm going through a cocos2D book and I'm trying to get a Radar Dish object initialized. The animations are set up in the initAnimations method (which is called in the init method), however the init method is never called. In the GameLayer.m they use a createObjectOfType method which calls:
RadarDish *radarDish = [[RadarDish alloc] initWithSpriteFrameName:@"radar_1.png"];
So initWithSpriteFrameName is an init of RadarDish's super class, and it sets up the Radar Dish. So when does RadarDish.m's init ever get called?? It's causing my program to crash because the animations never get set up in the init.
Upvotes: 0
Views: 125
Reputation: 64012
You need to override initWithSpriteFrameName:
in any subclass, including RadarDish
, which needs its own initialization steps. In that method, you need to call up to the superclass's designated initializer and then continue with your subclass's particular needs, like so:
- (id)initWithSpritFrameName: (NSString *)frameName
{
self = [super initWithSpriteFrameName:frameName];
if( !self ) return nil;
[self initAnimations]; // As long as initAnimations doesn't also call a
// superclass's initializer!
// Other setup...
return self;
}
Now [[RadarDish alloc] initWithSpriteFrameName:...]
will use this implementation, ensuring the animations are set for your object.
You also should change the name of initAnimations
, because Cocoa convention is that only actual instance initializers -- methods which take a "raw" instance and prepare the instance fully -- should begin with init
. Call it setUpAnimations
or something like that.
Upvotes: 3