簡體   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