initialization - Passing level with SKTransistion issue -


when making transition new scene, (i calling instance of self new scene), trying set level number property of new instance.

problem when instance first created initwithsize called before can set level property, , can set level property after instance created, therefore property level set default (0) when initwithsize first called.

 myscene *destinationscene = [[myscene alloc]init];      destinationscene.currentlevel = (int) level;      nslog(@"519 level passed: %d new level: %d", (int)level, destinationscene.currentlevel);      sktransition *transtition = [sktransition doorwaywithduration:2];     [self.view presentscene:destinationscene transition:transtition]; 

initwithsize: check level number here , load

 _currentlevel = self.currentlevel;          // check if no level ie loading game first  time         if (_currentlevel==0) {             _currentlevel=1;         }          [self loadlevel:_currentlevel]; 

only way around have found call initwithsize twice uses memory , messy. feedback appreciated.

you want decouple game state scenes. create class gamestate (add prefix appropriate). have 2 choices here. can create global instance of game state, or singleton access game state.

something (this has defined singleton class method):

@interface gamestate : nsobject  @property (nonatomic, assign) nsinteger currentlevel;  // add other properties here  + (instancetype)sharedinstance;  @end 

you can load level later using like:

[newscene loadlevel:[gamestate sharedinstance].currentlevel]; 

the benefit of can access things currentlevel other essential items score, lives, etc 1 common instance.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -