使用触摸手势移动 SpriteKit 节点每次新触摸只会移动一个点

Moving SpriteKit Node with Touch Gestures only moves one point with each new touch

我有一个 SpriteKitNode,当我触摸它并拖动它时,我想在我的场景中移动它。

我有一个 touchesBegan 方法来检测我要移动的特定节点是否被触摸:

var selectionBoxIsTouched: Bool!
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches{
            let location = touch.location(in: self.canvasScene)
            canvasScene.enumerateChildNodes(withName: "Selection_Box", using:
                                                { [self] (node, stop) -> Void in
                if self.canvasScene.atPoint(location) == node {
                    selectionBoxIsTouched = true
                } else {
                    selectionBoxIsTouched = false
                }
            })
        }
    } 

接下来,我有 touchesMoved 方法,如果当前正在触摸我的节点,它会随着用户在屏幕上移动手指而移动:

  
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if selectionBoxIsTouched {
            if let touch = touches.first {
                let touchLoc = touch.location(in: self.canvasScene)
                let prevTouchLoc = touch.previousLocation(in: self.canvasScene)
                
                canvasScene.enumerateChildNodes(withName: "Selection_Box", using:
                                                    { (node, stop) -> Void in
                    if let touchedNode = node as? SKShapeNode {
                        let newYPos = touchedNode.position.y + (touchLoc.y - prevTouchLoc.y)
                        let newXPos = touchedNode.position.x + (touchLoc.x - prevTouchLoc.x)
                        touchedNode.position = CGPoint(x: newXPos, y: newYPos)  //set new X and Y for your sprite.
                    }
                })
            }
        }
    }

当我 select 节点 + 尝试移动它时,它不会连续移动....它移动少量,然后停止;即使我的手指一直在屏幕上移动。

我该如何解决这个问题,以便节点可以随着我的手指平滑、连续地移动?

此触摸代码应该适合您

extension GameScene {

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches{
            let location = touch.location(in: self)
            let _ = self.nodes(at: location).map { ([=10=] as? DraggableNode)?.isPicked = true }
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches{
            let touchLoc = touch.location(in: self)
            let prevTouchLoc = touch.previousLocation(in: self)
            let picked_nodes = self.children.filter { ([=10=] as? DraggableNode)?.isPicked ?? false }
            for node in picked_nodes {
                let deltaX = touchLoc.x - prevTouchLoc.x
                let deltaY = touchLoc.y - prevTouchLoc.y
                node.position.x += deltaX
                node.position.y += deltaY
            }
        }
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let _ = self.children.map { ([=10=] as? DraggableNode)?.isPicked = false }
    }
}

结合以下SKScene。我建议将您的选择器标志放在可移动节点内,这样您就可以拖动单个节点而不是进行单个全局测试。

class DraggableNode: SKNode {
    var isPicked:Bool = false
    override init() {
        super.init()
        let shape = SKShapeNode(circleOfRadius: 50)
        shape.fillColor = .red
        shape.name = "Selection_Box"
        self.addChild(shape)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class GameScene: SKScene {
    let draggableNodeA = DraggableNode()
    let draggableNodeB = DraggableNode()

    override func didMove(to view: SKView) {
        draggableNodeA.position.x = 50
        draggableNodeB.position.x = -50
        self.addChild(draggableNodeA)
        self.addChild(draggableNodeB)
    }
}

问题最终是我有一个与多点触控委托方法冲突的平移手势识别器。我通过向 enable/disable 添加逻辑来修复我想要移动的特定节点是否被触摸的手势识别器。