SKAction 淡出、更改文本、淡入

SKAction FadeOut, Change text, FadeIn

我正在为一款游戏制作介绍独白。我想知道如何在包含文本更改的序列中使用 SKActions。

我目前有

monologue.runAction(SKAction.fadeOutWithDuriation(1.5))
monologue.text = "text2"
//*wait for tap* (coded in)
monologue.runAction(SKAction.fadeInWithDuriation(1.5))
extension SKNode
{
    func runAction( action: SKAction!, withKey: String!, optionalCompletion: dispatch_block_t? )
    {
        if let completion = optionalCompletion
        {
            let completionAction = SKAction.runBlock( completion )
            let compositeAction = SKAction.sequence([ action, completionAction ])
            runAction( compositeAction, withKey: withKey )
        }
        else
        {
            runAction( action, withKey: withKey )
        }
    }
}

代码:

var isMonologueReady : Bool = false //global var

monologue.name = "monologue"
self.isMonologueReady = false
let fadeOut = SKAction.fadeOutWithDuration(1.5)
let wait = SKAction.waitForDuration(1.5)
let changeText = SKAction.runBlock({monologue.text = "text2"})
let changeMytext = SKAction.group([wait,changeText])
let actionSequence = SKAction.sequence([fadeOut,changeMytext])
monologue.runAction(actionSequence,withKey:"monologueSequence",optionalCompletion: {
   self.isMonologueReady = true
})

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        /* Called when a touch begins */

        let touch = touches.first
        let positionInScene = touch!.locationInNode(self)

        let touchedNode = self.nodeAtPoint(positionInScene)
        print (touchedNode.name)
        if let name = touchedNode.name
        {
            if name == "monologue" && self.isMonologueReady
            {
                let fadeIn = SKAction.fadeInWithDuration(1.5)
                monologue.runAction(fadeIn,withKey:"monologueFadeIn") 
            }
        }
}

解释:

创建所有基本动作后,您可以选择必须按顺序 运行 将哪些动作组合在一起。正如您在这段代码中看到的,waitchangeText 是成组启动的(并行模式,一起..),在您可以看到基于 fadeOut 和 SKAction.group 名为 changeMytext。最后,在 tap 之后,如果 actionSequence 终止(布尔值为真),那么可以 运行 fadeIn.