繁体   English   中英

使用触摸手势移动 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 { ($0 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 { ($0 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 { ($0 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)
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM