2016-02-19 94 views
2

我很困惑這個循環在我的Swift代碼中不起作用。這裏的功能的全部 - 「pulseChar」是給我的錯誤「索引超出範圍」:Swift for loop not working

func openingSequence(){ 
     let nodeList = [self.drum,self.piano,self.guitarBoard] 
     let offset = SKAction.waitForDuration(10) 
     let stOne = SKAction.runBlock {() -> Void in 
      for var i in 0...nodeList.count-1{ 
       nodeList[i].runAction(SKAction.fadeAlphaTo(0.3, duration: 0.3)) 
       i++ 
      } 
     } 
     let firstLineWait = SKAction.waitForDuration(4) 
     let moveSprites = SKAction.runBlock {() -> Void in 
      moveScale(self.rheaBoard, duration: 0.5, to: CGPoint(x: 100, y: self.frame.height - 85), size: 0.4) 
      moveScale(self.guitarBoard, duration: 0.5, to: CGPoint(x: self.frame.midX - self.frame.midX/2, y: 65), size: 0.35) 
      for var i in 0...nodeList.count-1{ 
       nodeList[i].runAction(fadeI) 
       i++ 
      } 
     } 
     let fadeAudio = SKAction.runBlock {() -> Void in 
      fadeOtherTracksOut([9,8,4,2,1]) 
     } 
     let moveFade = SKAction.group([moveSprites,fadeAudio]) 
     let pulseChar = SKAction.runBlock {() -> Void in 
      for var i in 0...nodeList.count-1{ 
       self.background.runAction(SKAction.runBlock({() -> Void in 
        pulse(nodeList[i], startScale: 0.35, endScale: 0.4) 
       })) 
       i++ 
      } 
      self.startInteaction = true 
     } 

     background.runAction(SKAction.sequence([offset,stOne,firstLineWait,moveFade,pulseChar])) 
    } 

我的編譯器告訴我,我= 3時失敗......但「我」應該拿不到三,作爲

nodeList.count = 3 

,我使用範圍

for var i in 0...nodeList.count-1 

而且,之前的工作就好了相同的循環......這是怎麼回事嗎?我是編程新手,所以如果這是一個非常簡單的解決方法,你將不得不原諒我。但幫助將不勝感激。

編輯

看來,運營商i的++需要是runBlock內:

let pulseChar = SKAction.runBlock {() -> Void in 
      for var i in 0...nodeList.count-1{ 
       self.background.runAction(SKAction.runBlock({() -> Void in 
        pulse(nodeList[i], startScale: 0.35, endScale: 0.4) 
        i++ 
       })) 
      } 

這解決了這個問題,但我真的很感激,如果有人不介意解釋爲什麼情況就是如此。在我看來,如果運算符i ++在循環內,它不應該== 3.

+0

另外值得一提的是,'我++語法'將斯威夫特 –

回答

4

不要將索引變量i「手動」增加,重複循環會自動執行該操作。

而且可以使用半開區間操作,以避免額外的減法,例如

for i in 0..<nodeList.count { 
    nodeList[i].runAction(SKAction.fadeAlphaTo(0.3, duration: 0.3)) 
} 

for node in nodeList { 
    node.runAction(SKAction.fadeAlphaTo(0.3, duration: 0.3)) 
} 

如果不需要的數字指標。

甚至

nodeList.forEach{ $0.runAction(SKAction.fadeAlphaTo(0.3, duration: 0.3)) } 
+0

阿確定的下一個版本中刪除!我沒有意識到我自動遞增。謝謝! – drockFai

+1

或'nodelist.forEach {...}' –

+0

我只是在編輯;-) @MartinR – vadian

0

由蘋果提供Check Here For More Details

護理完全看下面的代碼,它的輸出迅速編程指南。

for index in 1...5 { 
    print("\(index) times 5 is \(index * 5)") 
} 
// 1 times 5 is 5 
// 2 times 5 is 10 
// 3 times 5 is 15 
// 4 times 5 is 20 
// 5 times 5 is 25 

In For循環中我們不需要增加計數器。 遞增部分由自己的循環處理

請嘗試下列更新的代碼。

func openingSequence(){ 
     let nodeList = [self.drum,self.piano,self.guitarBoard] 
     let offset = SKAction.waitForDuration(10) 
     let stOne = SKAction.runBlock {() -> Void in 
      for var i in 0..<nodeList.count{ 
       nodeList[i].runAction(SKAction.fadeAlphaTo(0.3, duration: 0.3)) 

      } 
     } 
     let firstLineWait = SKAction.waitForDuration(4) 
     let moveSprites = SKAction.runBlock {() -> Void in 
      moveScale(self.rheaBoard, duration: 0.5, to: CGPoint(x: 100, y: self.frame.height - 85), size: 0.4) 
      moveScale(self.guitarBoard, duration: 0.5, to: CGPoint(x: self.frame.midX - self.frame.midX/2, y: 65), size: 0.35) 
      for var i in 0..<nodeList.count{ 
       nodeList[i].runAction(fadeI) 

      } 
     } 
     let fadeAudio = SKAction.runBlock {() -> Void in 
      fadeOtherTracksOut([9,8,4,2,1]) 
     } 
     let moveFade = SKAction.group([moveSprites,fadeAudio]) 
     let pulseChar = SKAction.runBlock {() -> Void in 
      for var i in 0..<nodeList.count{ 
       self.background.runAction(SKAction.runBlock({() -> Void in 
        pulse(nodeList[i], startScale: 0.35, endScale: 0.4) 
       })) 

      } 
      self.startInteaction = true 
     } 

     background.runAction(SKAction.sequence([offset,stOne,firstLineWait,moveFade,pulseChar])) 
    }