使用SpriteKit一次移动多个节点

Cyr*_*cia 1 touchesmoved touchesbegan sprite-kit

我有一个游戏,有3个SKSpriteNodes用户可以使用touchesBegan和移动touchesMoved.但是,当用户移动nodeA并通过另一个被调用的节点时nodeB,nodeB跟随nodeA等等.

我创建了一个数组SKSpriteNodes并使用a for-loop来简化生活.

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {


        let nodes = [nodeA, nodeB, nodeC]

        for touch in touches {

            let location = touch.location(in: self)

            for node in nodes {

                if node!.contains(location) {

                    node!.position = location
                }

            }


        }

    }
Run Code Online (Sandbox Code Playgroud)

一切正常,除非在nodeA移动和交叉路径时nodeB,nodeB遵循nodeA.

我怎样才能让这个当用户移动nodeAnodeA通过传递nodeBnodeB将不遵循nodeA.

Kni*_*gon 5

不要进行慢速搜索,而是为触摸的节点设置一个特殊的节点变量

class GameScene
{
    var touchedNodeHolder : SKNode?

    override func touchesBegan(.....)
    {
        for touch in touches {
            guard touchNodeHandler != nil else {return} //let's not allow other touches to interfere
            let pointOfTouch = touch.location(in: self)
            touchedNodeHolder = nodeAtPoint(pointOfTouch)
        }
    }
    override func touchesMoved(.....)
    {
        for touch in touches {

            let pointOfTouch = touch.location(in: self)
            touchedNodeHolder?.position = pointOfTouch
        }
    }
    override func touchesEnded(.....)
    {
        for touch in touches {

            touchedNodeHolder = nil
        }
    }
}
Run Code Online (Sandbox Code Playgroud)