【问题标题】:Moving multiple nodes at once from touch with SpriteKit使用 SpriteKit 一次移动多个节点
【发布时间】:2016-12-16 07:31:32
【问题描述】:

我有一个游戏,有 3 个SKSpriteNodes,用户可以使用touchesBegantouchesMoved 在其中移动。但是,当用户移动nodeA 并通过另一个名为nodeB 的节点时,nodeB 会跟随nodeA 等等。

我创建了一个SKSpriteNodes 数组,并使用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
                }

            }


        }

    }

一切正常,除非nodeA 移动并与nodeB 交叉路径,nodeB 跟随nodeA

我怎样才能使当用户移动nodeAnodeA 通过nodeBnodeB 不会跟随nodeA

【问题讨论】:

    标签: sprite-kit touchesbegan touchesmoved


    【解决方案1】:

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

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

    【讨论】:

    • 如果您打算使用多点触控,则需要在触控结束时循环来区分不同的触控,您可能需要添加此检查
    • 这是一种更好的方法,但是我不想让用户触摸的所有其他节点都允许它们。
    • 没关系,我找到了解决方法。谢谢你的回答!
    • 如果您希望节点不支持触摸,请为各个节点启用 userInteractionEnabled。我知道逻辑可能看起来倒退,但想法是不要让场景触摸开始方法火。我不确定当您滑过它们时触摸动作会如何起作用,您必须进行测试。
    【解决方案2】:

    经过一番试验,我找到了解决这个问题的方法。

    我发现不是通过触摸位置来选择节点,而是通过名称属性来选择节点。

    显示的代码在 touchesMoved 和 touchesBegan 方法中实现

    //name of the nodes
        let nodeNames = ["playO", "playOO", "playOOO", "playX", "playXX", "playXXX"]
    //the actual nodes
        let player = [playerO, playerOO, playerOOO, playerX, playerXX, playerXXX]
    
            for touch in touches {
    
                let pointOfTouch = touch.location(in: self)
                let tappedNode = atPoint(pointOfTouch)
                let nameOfTappedNode = tappedNode.name
    
                for i in 0..<nodeNames.count {
    
                    if nameOfTappedNode == nodeNames[i] {
                        z += 1
                        player[i]!.zPosition = CGFloat(z)
                        print(player[i]!.zPosition)
                        player[i]!.position = pointOfTouch
                    }
                }
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多