Swift spritekit set userdata
In my quick spritekit game I want to set a custom value in the sprite, I am trying to do it like this
p.userData?.setValue(value: "Hello", forKey: "c")
But when I try to do it, it says "Couldn't find an overload for 'setValue' that takes the supplied arguments."
So the main question is:
How do I set a custom value in a sprite?
+3
source to share
1 answer
The first argument in the method has no external parameter name, so it must be
p.userData?.setValue("Hello", forKey: "c")
or better (since userData
is NSMutableDictionary
and does not encode the key value, magic is required here):
p.userData?.setObject("Hello", forKey: "c")
Note also (as noted in the comment) that you must create the dictionary first:
p.userData = NSMutableDictionary()
p.userData?.setObject("Hello", forKey: "c")
or, alternatively, assign the dictionary with your keys and values:
p.userData = ["c" : "Hello"]
+13
source to share