SKAction FadeOut,更改文本,淡入淡出

时间:2016-06-07 09:20:04

标签: swift skaction

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

我目前有

monologue.runAction(SKAction.fadeOutWithDuriation(1.5))
monologue.text = "text2"
//*wait for tap* (coded in)
monologue.runAction(SKAction.fadeInWithDuriation(1.5))

1 个答案:

答案 0 :(得分:0)

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序列和{ {1}}名为SKAction.group。最后,点击后,如果changeMytext被终止(布尔值为true),那么它可以运行actionSequence