1

I have previously spent a lot of time using cocos2d, and got used to the convenience of tags, which are also available on UIView objects, but currently I started a project using spritekit and cannot find an equivalent. Thus my question, does anyone know if there is any kind of equivalent to it for SKSpriteNode/SKNode objects?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Rich Fox
  • 2,194
  • 18
  • 20

2 Answers2

5

Sprite got something way better. Instead of just a tag, within a SKNode you can set a whole dictionary. Say hello to your new friend .userData?

Example:

   let myNode : SKSpriteNode = SKSpriteNode(imageNamed: "make-america-great-again")
   myNode.userData = [
            "keyA" : "blahblah",
            "keyB" : 17.0
        ];

   NSLog("OMFG is this \(myNode.userData?["keyA"]) and \(myNode.userData?["keyB"]) for real!??")
bicycle
  • 8,315
  • 9
  • 52
  • 72
  • 1
    Im using ObjC still. But thanks for this info. Pretty useful property! – GeneCode Mar 04 '16 at 18:16
  • @Rocotilos You can do this in objective C also. Check http://stackoverflow.com/questions/19073199/sprite-kit-ios7-sknode-userdata-property-not-storing-values – bicycle Mar 04 '16 at 20:07
4

You can use the name property of SKNode.

From the documentation:

Used to assign a name to a node for later reference. When choosing a name for a node, you should decide whether each node gets a unique name or whether some nodes will share a common name. If you give the node a unique name, you can find the node later by calling the childNodeWithName: method. If a name is shared by multiple nodes, the name usually means that these are all a similar object type in your game. In this case, you can iterate over those objects by calling the enumerateChildNodesWithName:usingBlock: method.

Marcelo
  • 9,916
  • 3
  • 43
  • 52