2016-06-14 9 views
-1

間違いがわかりません。この問題を解決するのを手伝ってください。致命的なエラーインデックスが範囲外になっています

これは私のコードです:

import SpriteKit 

class EncounterManager { 
    let encounterNames: [String] = ["EncounterName1", 
            "EncountersName2", 
            "EncountersName3"] 
    var encounters: [SKNode] = [] 

    var currentEncounterIndex:Int? 

    var previousEncounterIndex:Int? 


    init() { 
     for encounterFileName in encounterNames { 
      let encounter = SKNode() 
      if let encounterScene = SKScene(fileNamed: encounterFileName) { 
       for placeholder in encounterScene.children { 

        if let node = placeholder as? SKNode { 

         switch node.name! { 
         case "N" : 
          let n = N() 
          n.spawn(encounter, position: node.position) 

         case "NN": 
          let nn = NN() 
          nn.spawn(encounter, position: node.position) 

         case "NNN": 
          let nnn = NNN() 
          nnn.spawn(encounter, position: node.position) 

         case "GGG": 
          let ggg = GGG() 
          ggg.spawn(encounter, position: node.position) 

         case "GG": 
          let gg = GG() 
          gg.spawn(encounter, position: node.position) 

         case "G": 
          let g = G() 
          g.spawn(encounter, position: node.position) 

         case "F": 
          let f = F() 
          ff.spawn(encounter, position: node.position) 

         default: 
          print("Name error: \(node.name)") 
        } 
       } 
      } 
     } 
    } 
} 

    func addToWorld(world:SKNode) { 

     for index in (encounters.count).stride(through: 0, by: -1) { 

      encounters[index].position = CGPoint(x: -2000, y: index * 1000) 
      world.addChild(encounters[index]) 

     } 

    } 

} 

これは、コンソールからのエラーです:

fatal error: Index out of range 

これはエラーにラインです:

encounters[index].position = CGPoint(x: -2000, y: index * 1000) 

これはラインエラーです:

EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP, subcode=0x0) 

インターネットでこの問題に関する情報を見つけようとしましたが、見つけられませんでした。

+2

*ヒント:* 'count'要素を持つ配列の最後の要素のインデックスとは何ですか? –

答えて

1

変更この行:これに

for index in (encounters.count).stride(through: 0, by: -1) 

for index in (encounters.count.predecessor()).stride(through: 0, by: -1) 

最後の有効な指標の背後にあるものですストライドがencounters.countから始まる最初のバージョンを使用しているのであなたがエラーを取得します。

+2

または: 'for index in encounters.indices.reverse()... ' –

+0

@ViktorSimkó最初のファイルで修正するのに役立ちますが、別のファイルにこのエラーがあります。 '' encounterManager.encounters [0] .position = CGPoint(x:300、y:30) ' – antoshkaaa

+0

@MartinR最初のファイルで修正するのに役立ちますが、今は別のファイルにこのエラーがあります。インラインで 'encounterManager.encounters [0] .position = CGPoint(x:300、y:30)' – antoshkaaa

関連する問題