【问题标题】:detecting touch on SKShapeNode doesn't work the same way as with sprites检测 SKShapeNode 上的触摸与使用 sprite 的方式不同
【发布时间】:2015-02-21 14:20:21
【问题描述】:

我有一个 SKShapeNode 并为它设置了一个名称,但是当我尝试在 touch 中检测到它时,它的工作方式与您与其他 sprite 一起使用的方式不同。

 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */

        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)



            if let body = self.nodeAtPoint(location) as? SKSpriteNode {

                if var name: String = body.name {
                    switch name {

                        case "myShape":
                             println("Shape Touched")

                        case "enemy":
                             println("Enemy Touched")

                    default:


                    }
                }
            }
        }
    }

enemy 是一个 SKSpriteNode,它被正确检测到,但形状是 SKShapeNode 却没有。我正确输入了字符串名称。

【问题讨论】:

    标签: sprite-kit touch skshapenode


    【解决方案1】:

    在使用 SpriteKit 检测触摸时,我发现使用 SKNode 的 contiansPoint() 方法比使用 nodeAtPoint() 更容易

    let myShape = self.childNodeWithName("myShape")
    if myShape.containtsPoint(location) {
        println("Shape touched")
    }
    

    显然,我的代码示例根本无法很好地扩展,但根据我的经验,总的来说 containsPoint() 比 nodeAtPoint() 可靠得多。

    【讨论】:

    • 我将形状更改为 spriteNode,它工作正常,看起来形状必须填充纹理或其他东西才能被检测到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多