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")
}
}
}
說明:
之後,你有創造的所有基礎動作,你可以選擇什麼樣的行動必須是r依次連在一起。如您在此代碼中看到的那樣,wait
和changeText
以組(並行模式,合起來)形式啓動,之後您可以看到基於fadeOut的SKAction
序列和名爲changeMytext
的SKAction.group
。最後,點擊後,如果actionSequence
終止(布爾值爲true),所以可以運行fadeIn
。