按位置

时间:2018-05-19 17:06:53

标签: ios swift sprite-kit skspritenode

我有很多SpriteNode:

sharesA.name = "sharesA"
sharesB.name = "sharesB"
sharesC.name = "sharesC"
ect.....

其中一位有一个位置,例如:

CGPoint(x: 1159, y: 406)

如何找出处于这个位置的精灵的名字?

1 个答案:

答案 0 :(得分:2)

如果您有坐标,可以尝试: let name = nodeAtPoint(location).name ?? ""

如果您想根据用户触摸来确定,可以尝试以下方式:

 class MyScene: SKScene {

     override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

        var touch = touches as!  Set<UITouch>
        var location = touch.first!.locationInNode(self)
        var node = self.nodeAtPoint(location)
        let nodeName = node.name
     }
 }