问题描述:

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?

网友答案:

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.

网友答案:

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!??")
相关阅读:
Top