SpriteKit:在场景之间传递数据 [英] SpriteKit: Passing data between scenes

查看:158
本文介绍了SpriteKit:在场景之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SpriteKit中尝试在场景SKScene之间传递数据时遇到困难。例如,我想将分数从A级传递到B级。

I am stuck trying to pass data between scenes "SKScene" in SpriteKit. For instance I would like to pass the score from level A to Level B.

也许解决方案是归档但我希望实现更简单的方式,就像我们使用的方式一样使用视图控制器。

Maybe the solution is archiving but I would like to implement something more simpler like the way we use with view controllers.

非常感谢这方面的任何线索。

Any clue in this regard will be very much appreciated.

推荐答案

如果您要将分数传递到许多不同的场景,您可能希望将其存储在 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];

 }

这篇关于SpriteKit:在场景之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆