Hey guys sorry for the noob question but I am having a hard time initializing my self created classes for an iphone game I’ve started making. Before I started actually developing my game I ran a quick test to see if what I have already would work, and the class that I was testing wouldn’t initialize. Here is what I have:
@implementation Game
SceneController *sceneController;
+(id)scene {...} // just the default scene method created by cocos2d
-(id)init{
if((self=[super init])){
[[sceneController performSelector:@selector(alloc)]performSelector:@selector(init)];
CGSize size = [[CCDirector sharedDirector] winSize];
CCLabelTTF *label=[CCLabelTTF labelWithString:[NSString stringWithFormat:@"%i",[sceneController performSelector:@selector(xxx)]] fontName:@"Marker Felt" fontSize:64];
[self addChild: label];
}
return self;
}
// IN ANOTHER CLASS
@implementation SceneController
int xxx; // not a real variable just used for the test
-(id)init{
if((self=[super init])){
xxx=432;
}
return self;
}
-(int)xxx{
return xxx;
}
My problem is that instead of the label reading 432 like it should it just says 0. Can anyone help me.
Looks like you haven’t contructed your
SceneControllerobject correctly.In your
initmethod of Game, you need to make a call like this:sceneController = [[SceneController alloc] init];instead of this:
[[sceneController performSelector:@selector(alloc)]performSelector:@selector(init)];Then your
sceneControllerinstance variable is properly initialised and yourinitmethod will have been called.