I'm trying to initialize a scene in cocos2d (version 0.99.5) and want to pass in a parameter. In this case, the parameter is an int value corresponding to the level number. The scene class itself is a subclass of CCLayer
, and I'm initializing it with the node class method:
GameScene *scene = [GameScene node]; //GameScene subclass of CCLayer
I have a custom init method that takes the variable "level", as follows:
- (id) initWithGameLevel:(int)level {
if ((self = [super init])){
// etc
}
}
Just wondering: am I way off base in my approach in creating my own initWithX method, and how I would initialize the scene with the level integer?
Add this method to your subclass
+(id)nodeWithGameLevel:(int)level{
return [[[self alloc] initWithGameLevel:level] autorelease];
}
and instead of
GameScene *scene = [GameScene node];
write
GameScene *scene = [GameScene nodeWithGameLevel:levelNumber];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With