我很困惑這個循環在我的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.
另外值得一提的是,'我++語法'將斯威夫特 –