通过SpriteKit一次移动多个节点

通过SpriteKit一次移动多个节点

本文介绍了通过SpriteKit一次移动多个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个具有3个SKSpriteNodes的游戏,用户可以使用touchesBegantouchesMoved进行移动.但是,当用户移动nodeA并经过另一个称为nodeB的节点时,nodeB跟随nodeA,依此类推.

I have a game that has 3 SKSpriteNodes that the user can move around using the touchesBegan and touchesMoved. However, when the users moves nodeA and passes another node called nodeB, nodeB follows nodeA and so on.

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

I created an array of SKSpriteNodes and used a for-loop to make life easier.

 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
                }

            }


        }

    }

一切正常,除了nodeA正在移动并且与nodeB交叉路径时,nodeB跟随nodeA.

Everything is working except when nodeA is moving and cross paths with nodeB, nodeB follows nodeA.

如何做到这一点,以便当用户移动nodeA并且nodeA经过nodeB时,nodeB不会跟随nodeA.

How can I make it so that when the user is moving nodeA and nodeA passes through nodeB that nodeB would not follow nodeA.

推荐答案

与其进行缓慢的搜索,不如为被触摸的节点提供特殊的节点变量

Instead of doing slow searches, have a special node variable for your touched node

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
        }
    }
}

这篇关于通过SpriteKit一次移动多个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:27