Jim McGaw
Jim McGaw

Reputation: 768

Initialize Scene in cocos2d with Parameter

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?

Upvotes: 6

Views: 3986

Answers (1)

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39978

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];

Upvotes: 19

Related Questions