Swift spritekit set userdata

后端 未结 1 450
温柔的废话
温柔的废话 2020-12-21 05:46

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\")

相关标签:
1条回答
  • 2020-12-21 06:25

    The first argument in a method does not have an external parameter name, therefore it should be

    p.userData?.setValue("Hello", forKey: "c")
    

    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"]
    
    0 讨论(0)
提交回复
热议问题