您可以将节点的位置保存在 sks 文件中吗 [英] Can you save the position of a node in an sks file

查看:29
本文介绍了您可以将节点的位置保存在 sks 文件中吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 SKScene 文件,在其中我们在放置玩家和其他对象的位置创建空节点.问题是所有位置都必须在代码中设置.是否可以在 sks 文件中设置位置?我确实注意到节点检查器中有一个位置标签,但这似乎没有任何效果.

I'm trying to create a SKScene file where we create empty nodes at the places where our players and other objects will be placed. The problem is that the positions all have to be set in code. Is it possible to set the position in the sks file instead? I did notice there is a Position label in the node inspector, but that doesn't appear to have any effect.

推荐答案

使用 NSUserDefaults:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSDictionary *nodeDict = [[NSDictionary alloc] initWithObjectsAndKeys:
                          [NSNumber numberWithFloat:myNode.position.x], @"posX",
                          [NSNumber numberWithFloat:myNode.position.y], @"posY",
                          nil];

[defaults setObject: nodeDict forKey:@"nodeDict"];

[defaults synchronize];

使用 NSUserDefaults 读取数据:

Read data with NSUserDefaults:

NSDictionary * nodeDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"nodeDict"];

float posX = [[nodeDict objectForKey:@"posX"] floatValue];
float posY = [[nodeDict objectForKey:@"posY"] floatValue];

显然,您可以写入比上面的示例多得多的数据.如果您有多个节点,请为每个节点创建一个字典并将它们全部存储在一个数组中.将数组写入 NSUserDefaults.

Obviously you can write a lot more data than the example above. If you have several nodes, create a dictionary for each one and store them all in an array. Write the array to NSUserDefaults.

请记住,您只能将对象存储在字典中,而不能将诸如 int、float 等原语存储...这就是您必须将它们转换为 NSNumber 的原因.

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.

这篇关于您可以将节点的位置保存在 sks 文件中吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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