第一个函数完成后,调用第二个函数使用Swift进行SKAction重复计数

时间:2015-10-23 13:36:47

标签: xcode swift ios9

我有这个功能:

func midGroundlevelMovingSlow() {

    let moveLevelImage = SKAction.moveByX(-self.frame.size.width, y: 0, duration: gameSpeed)
    let replaceLevelImage = SKAction.moveByX(self.frame.size.width, y: 0, duration: 0)
    let movingAndReplacingLevelImage = SKAction.repeatAction(SKAction.sequence([moveLevelImage,replaceLevelImage]), count: 5)

    mgImage.runAction(movingAndReplacingLevelImage)
    mgImage2.runAction(movingAndReplacingLevelImage)
}

一旦完成它的循环5,我希望调用此函数:

func midGroundlevelMovingMedium() {

    let moveLevelImage = SKAction.moveByX(-self.frame.size.width, y: 0, duration: gameSpeed * 2)
    let replaceLevelImage = SKAction.moveByX(self.frame.size.width, y: 0, duration: 0)
    let movingAndReplacingLevelImage = SKAction.repeatAction(SKAction.sequence([moveLevelImage,replaceLevelImage]), count: 5)

    mgImage.runAction(movingAndReplacingLevelImage)
    mgImage2.runAction(movingAndReplacingLevelImage)
}

有人可以帮我解决这个问题吗?

谢谢,

史蒂芬

1 个答案:

答案 0 :(得分:1)

SKSpriteNode具有带有完成处理程序的运行操作函数。

sprite.runAction(action: SKAction) { () -> Void in
        // call next function
    }

您可以在该完成处理程序中运行您的下一个方法。

在您要等待两个功能完成的情况下,您可以使用计数器并在调用下一个方法之前检查两者是否都已完成。

类似

func midGroundlevelMovingSlow(completion: (()->Void)?) {

    let moveLevelImage = SKAction.moveByX(-self.frame.size.width, y: 0, duration: gameSpeed)
    let replaceLevelImage = SKAction.moveByX(self.frame.size.width, y: 0, duration: 0)
    let movingAndReplacingLevelImage = SKAction.repeatAction(SKAction.sequence([moveLevelImage,replaceLevelImage]), count: 5)

    var callCount = 2

    mgImage.runAction(movingAndReplacingLevelImage){ () -> Void in
        if --callCount == 0 {
            completion?()
        }
    }

    mgImage2.runAction(movingAndReplacingLevelImage){ () -> Void in
        if --callCount == 0 {
            completion?()
        }
    }
}