Swift nodeAtPoint难度

时间:2014-08-10 05:17:09

标签: swift sprite-kit

我正在构建一个游戏,其中玩家在游戏板周围拖动一块。我想知道那件作品下面的所有节点是什么,我得到奇怪的结果。这是touchesMoves func:

    override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) {

        let touch = touches.anyObject() as UITouch
        let location = touch.locationInNode(self.parent)

        self.position = location
        println("checker x: \(location.x)")
        println("node at point: \(self.nodeAtPoint(location))")
        println("nodes at point: \(self.nodesAtPoint(location))")

    }

精灵在电路板周围移动很好,但是报告为nodeAtPoint的是总是被移动的精灵(哪种有意义但没有用。奇怪的是,nodesAtPoint总是被报告为空数组!这怎么可能?我应该采取哪些不同的做法?

更新:这仍然是一场斗争。我想将触摸方法保留在节点本身,而不是场景中。 touchesMoved的最新版本如下:

override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) {

    // Turn off the touched bool:
    touched = false

    let touch = touches.anyObject() as UITouch
    let location = touch.locationInNode(self.parent)
    let loc = touch.locationInView(scene.view)
    let loc2 = touch.locationInNode(scene)

    self.position = location
    println("checker: \(loc2.x), \(loc2.y)")
    println("node at point: \(self.nodeAtPoint(loc2).name)")
    println("nodes at point: \(self.nodesAtPoint(loc2))")

}

nodesAtPoint数组继续为空,有一个非常奇怪的异常。当徘徊在场景中心附近时,我明白了:

点上的节点:[名称:'(null)' accumulationFrame:{{ - 30,-19.80000114440918},{60,39.600002288818359}}]

我所知道的那里没有形状节点!为什么我没有检测到我传过的节点?

2 个答案:

答案 0 :(得分:2)

我发现了答案。基本上我试图检测自己的nodesAtPoint,在这种情况下是在屏幕上移动的节点。一旦我将其更改为self.scene,则nodesAtPoint数组按预期填充。所以,要明确:

println("nodes at point: \(self.nodesAtPoint(loc2))")

需要改为:

println("nodes at point: \(self.scene.nodesAtPoint(loc2))")

答案 1 :(得分:1)

如果selfSKScene,请尝试更改

let location = touch.locationInNode(self.parent)

let location = touch.locationInNode(self)

因为SKScene的父级是nil