2016-06-07 99 views
1

我正在爲遊戲創建一個簡介獨白。我想知道如何在包含文本更改的序列中使用SKActions。SKAction FadeOut,更改文本,淡入淡出

我現在有

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

回答

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") 
      } 
     } 
} 

說明

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