Как мне дождаться окончания действия, чтобы выполнить функцию в SpriteKit? - PullRequest
0 голосов
/ 04 мая 2019

Я создал действие, которое перемещает положение узла.

Я хочу изменить сцену после окончания действия.

Что я могу сделать?

Я пытался выставить временную позицию! = 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 голосов
/ 04 мая 2019

РЕШИТЬ !!!

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()})
            }
        }
    }
}
...