【发布时间】:2016-06-14 11:20:58
【问题描述】:
我不明白哪里出错了。请帮我解决这个问题。
这是我的代码:
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元素的数组中最后一个元素的索引是多少?