且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SpriteKit:在场景之间传递数据

更新时间:2022-06-27 00:08:57

如果您要将分数传递到许多不同的场景,您可能希望将其存储在 NSUserDefaults 或某些可访问的存储机制中。但是,如果您希望在SpriteKit对象之间传递数据,则每个 SKNode (包括 SKScene )都有一个字典属性名为 userData ,可以用于任何你想要的东西。以下是如何在场景之间传递分数的示例:

If you're going to be passing around the score to a lot of different scenes, you might want to store it in NSUserDefaults or some accessible storage mechanism. However, if you're looking to pass data between SpriteKit objects, every SKNode (including SKScene) has a dictionary property called userData that you can use for whatever you so desire. Here's an example of how you might pass the score between scenes:

 - (void)changeScene
 {
      SKView *spriteView = (SKView *) self.view;
      SKScene *currentScene = [spriteView scene];
      SKScene *newScene = [MySceneClass scene];
      [newScene.userData setObject:[currentScene.userData objectForKey:@"score"] forKey:@"score"];
      [spriteView presentScene:newScene];

 }