Passing data from one SKScene to the next

老子叫甜甜 提交于 2019-12-12 04:55:43

问题


So I am designing a game. In my MenuScene class I have the 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 to the game scene. I need to pass some data to the game scene. In the game scene, which is an instance of MyScene, I have

@interface MyScene()

@property (nonatomic) SKSpriteNode *heli;
@property (nonatomic) int health;
@property (nonatomic) float currentDY;
@property (nonatomic) float gameSpeed;



@end

And I want to be able to set these properties as I enter the new scene, based on input from the user from the old scene. In my research I have come to the conclusion that I will either want to use initWithCoder or some other sort of custom method to transition to the scene. Do I need to add a new method to my interface and then implement it in the implementation? How do I need to change my game scene to accept this new data? Do I need to replace initWithSize with my new method, let's say it is initWithSize:andFuel:?

Could someone please provide a simple example of how this would be done, in both the implementation and the interface?


回答1:


I personally have found NSUserDefaults to be the simplest solution to this.

To use your example, you can store health, currentDY and game speed in a dictionary like 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 that you can only store objects in a dictionary and not primitives like int, float, etc... That is why you have to convert them into NSNumber.

To get get them back out during the init method, you do 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 a SKSpriteNode in a dictionary and then storing it with NSUserDefaults. If it does not work, you will have to store all of the sprite's properties such as location, texture name, physics body specs, etc.. into a dictionary. You can then simply create a new sprite based on the stored values when you need it.




回答2:


I would like to point out that I have solved my own question, but I will give credit to the answerer above because he presented another solution. My solution makes more sense to me, however.

All I did was:

In my game scene, I created a new method initWithSize: fuel: and called if (self = [super initWithSize:size]) { the same way initWithSize does. Then, in my menu class, I replaced

MyScene *gameScene = [MyScene sceneWithSize:self.size];

With MyScene *gameScene = [MyScene alloc];

And then gameScene = [gameScene initWithSize:self.frame.size fuel:1];

I haven't had any issues with it so far - could someone tell me if I am doing anything incorrectly here? This seems to allow me to pass info into my next scene by creating a custom init method.



来源:https://stackoverflow.com/questions/25354504/passing-data-from-one-skscene-to-the-next

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!