如何等待动作结束才能在SpriteKit中执行功能?

时间:2019-05-04 17:05:50

标签: swift sprite-kit skaction

我创建了一个移动节点位置的动作。

我想在动作结束后改变场景。

我该怎么办?

我试图放一会儿位置!= targetPosition,但这会阻止操作。

func moveBattleScene(node: SKNode) {
    let width = frame.size.width
    let rectSize = width / 4
    let moveLeft = SKAction.moveBy(x: -rectSize * 2, y: 0, duration: 1)
    node.run(moveLeft)
 }

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        let nodeB = self.childNode(withName: "BATTLEBUTTON")
        let frameB = nodeB!.calculateAccumulatedFrame()
        let minX = frameB.minX
        let maxX = frameB.maxX
        let minY = frameB.minY
        let maxY = frameB.maxY
        if location.x > minX && location.x < maxX {
            if location.y > minY && location.y < maxY {
                changeLabel(labelNode: nodeB?.childNode(withName: "B") as! SKLabelNode)
                moveBattleScene(node: self.childNode(withName: "BATTLEBUTTON")!)
                self.childNode(withName: "BATTLEBUTTON")!.action
                let width = frame.size.width
                let rectSize = width / 4
                let finalPos = CGPoint(x: -rectSize * 2, y: 0)
                if self.childNode(withName: "BATTLEBUTTON")!.position == finalPos {
                    let gameScene = GameScene(size: view!.bounds.size)
                    view!.presentScene(gameScene)
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

解决了!

func changeScene() {
    let gameScene = GameScene(size: view!.bounds.size)
    view!.presentScene(gameScene)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        let nodeB = self.childNode(withName: "BATTLEBUTTON")
        let frameB = nodeB!.calculateAccumulatedFrame()
        let minX = frameB.minX
        let maxX = frameB.maxX
        let minY = frameB.minY
        let maxY = frameB.maxY
        if location.x > minX && location.x < maxX {
            if location.y > minY && location.y < maxY {
                changeLabel(labelNode: nodeB?.childNode(withName: "B") as! SKLabelNode)
                let width = frame.size.width
                let rectSize = width / 4
                let moveLeft = SKAction.moveBy(x: -rectSize * 2, y: 0, duration: 1)
                nodeB!.run(moveLeft, completion: {self.changeScene()})
            }
        }
    }
}
相关问题