Swift spritekit 设置用户数据 [英] Swift spritekit set userdata
问题描述
在我的 swift spritekit 游戏中,我想在精灵上设置自定义值,我尝试这样做
In my swift spritekit game I want to set a custom value on a sprite I try to do this like so
p.userData?.setValue(value: "Hello", forKey: "c")
但是当我尝试这个时,它说找不到接受提供的参数的 'setValue' 的重载".
But when I try this it says "Could not find an overload for 'setValue' that accepts the supplied arguments".
所以主要问题是:
如何在精灵上设置自定义值?
How can I set a custom value on a sprite?
推荐答案
方法中的第一个参数没有外部参数名,因此应该是
The first argument in a method does not have an external parameter name, therefore it should be
p.userData?.setValue("Hello", forKey: "c")
或更好(因为 userData
是 NSMutableDictionary
并且没有键值编码这里需要魔法):
or better (since userData
is a NSMutableDictionary
and no Key-Value Coding
magic is required here):
p.userData?.setObject("Hello", forKey: "c")
还要注意(正如评论中提到的)您必须创建字典第一:
Note also (as just mentioned in a comment) that you have to create the dictionary first:
p.userData = NSMutableDictionary()
p.userData?.setObject("Hello", forKey: "c")
或者,使用您的键和值分配字典:
or alternatively, assign a dictionary with your keys and values:
p.userData = ["c" : "Hello"]
这篇关于Swift spritekit 设置用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!