ios - Passing data from one SKScene to the next -


so designing game. in menuscene class have following:

-(void)touchesbegan:(nsset *)touches withevent:(uievent *)event { sktransition *transition = [sktransition fadewithcolor:[uicolor colorwithred:147.0/255.0 green:213.0/255.0 blue:216.0/255.0 alpha:1.0] duration:2.0f]; myscene *gamescene = [myscene scenewithsize:self.size]; [self.view presentscene:gamescene transition:transition]; }

which transitions game scene. need pass data game scene. in game scene, instance of myscene, have

@interface myscene()  @property (nonatomic) skspritenode *heli; @property (nonatomic) int health; @property (nonatomic) float currentdy; @property (nonatomic) float gamespeed;    @end 

and want able set these properties enter new scene, based on input user old scene. in research have come conclusion either want use initwithcoder or other sort of custom method transition scene. need add new method interface , implement in implementation? how need change game scene accept new data? need replace initwithsize new method, let's initwithsize:andfuel:?

could please provide simple example of how done, in both implementation , interface?

i have found nsuserdefaults simplest solution this.

to use example, can store health, currentdy , game speed in dictionary so:

nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults];  nsdictionary *settingsdict = [[nsdictionary alloc] initwithobjectsandkeys:                               [nsnumber numberwithint:health], @"health",                               [nsnumber numberwithfloat: currentdy], @"currentdy",                               [nsnumber numberwithfloat: gamespeed], @"gamespeed",                               nil];  [defaults setobject: settingsdict forkey:@"settingsdict"];  [defaults synchronize]; 

keep in mind can store objects in dictionary , not primitives int, float, etc... why have convert them nsnumber.

to get them out during init method, this:

nsdictionary *settingsdict = [[nsuserdefaults standarduserdefaults] objectforkey:@"settingsdict"];  int health = [[settingsdict objectforkey:@"health"] intvalue]; float currentdy = [[settingsdict objectforkey:@"currentdy"] floatvalue]; float gamespeed = [[settingsdict objectforkey:@"gamespeed"] floatvalue]; 

i have never tried storing skspritenode in dictionary , storing nsuserdefaults. if not work, have store of sprite's properties such location, texture name, physics body specs, etc.. dictionary. can create new sprite based on stored values when need it.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -