間違いがわかりません。この問題を解決するのを手伝ってください。致命的なエラーインデックスが範囲外になっています
これは私のコードです:
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)
インターネットでこの問題に関する情報を見つけようとしましたが、見つけられませんでした。
*ヒント:* 'count'要素を持つ配列の最後の要素のインデックスとは何ですか? –