Initialize scene in cocos2d with parameter

I am trying to initialize a scene in cocos2d (version 0.99.5) and want to pass 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 initialize it using the node class method:

 GameScene *scene = [GameScene node]; //GameScene subclass of CCLayer 

I have my own initialization method, which accepts the variable "level" as follows:

 - (id) initWithGameLevel:(int)level { if ((self = [super init])){ // etc } } 

Just curious: I'm not at the heart of my approach in creating my own initWi method, thanks, and how to initialize a scene using an integer?

+6
source share
1 answer

Add this method to your subclass.

 +(id)nodeWithGameLevel:(int)level{ return [[[self alloc] initWithGameLevel:level] autorelease]; } 

and instead

 GameScene *scene = [GameScene node]; 

to write

 GameScene *scene = [GameScene nodeWithGameLevel:levelNumber]; 
+19
source

Source: https://habr.com/ru/post/885781/


All Articles