检测SKNode上的触摸事件(Swift)

3

我已经创建了一个容器节点,将所有需要在一次触摸中移动的SKSpriteNodes放入其中,我可以正常地在iOS 8上检测到它们的触摸,但是在iOS 7上,我只能检测到主节点上的触摸,当我触摸容器节点中的SKSpriteNode时,什么也没有发生。如何解决这个问题?

let lvlsNode = SKNode()



override func didMoveToView(view: SKView) {

            self.addChild(lvlsNode)

            axe = SKSpriteNode(imageNamed:"axe")
            axe.anchorPoint = CGPointMake(1, 0)
            axe.size = CGSizeMake(axe.size.width/1.4, axe.size.height/1.4)
            axe.position = CGPointMake(0+screenWidth/7, shield.position.y-shield.size.width*1.4)
            axe.zPosition = 12
            axe.name = "axe"
            lvlsNode.addChild(axe)
}
 override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        /* Called when a touch begins */

        for touch in (touches as! Set<UITouch>) {
            let location = touch.locationInNode(self)
            let node = nodeAtPoint(location)

          if node.name == "axe" { 
             // do something.... this work in iOS8 but not in iOS 7.1
           }

1
当您记录节点时会发生什么?它是空的吗?请在“let node = nodeAtPoint(location)”下方放置一个断点并检查节点。它显示什么? - Philip
它显示了在斧子后面的SKSpriteNode,如果我打印(node.name),则对于容器节点中的每个SpriteNode都会得到nil。 - Ludyem
好的,那是因为您实际上正在按下该节点。它是什么类型的节点?您能将斧头附加到该节点上吗?如果可以,您可以检查node.name == nil,然后检查node.child/parent.name并查看是否为斧头名称。 - Philip
1个回答

0
Yournode.name = "nodeX"

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
   let touch = touches.first as UITouch!
   if atPoint((touch?.location(in: self))!).name == Yournode.name {
       //Your code
   }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接