Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 759
Jim McGaw Avatar asked Apr 13 '11 04:04

Jim McGaw


1 Answers

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];
like image 138
Inder Kumar Rathore Avatar answered Oct 02 '22 12:10

Inder Kumar Rathore