【发布时间】:2015-03-07 08:15:21
【问题描述】:
我有一个 Swift SpriteKit 场景,但并非每次都会发生,但我通过按菜单对重启游戏按钮进行了一些测试,并反复快速连续重启直到它崩溃。它还会从加载场景的其他点随机崩溃。我的估计是大约有 30% 的几率会在这条线上崩溃
类 AppDelegate: UIResponder, UIApplicationDelegate {
出现以下错误:
2015-03-07 09:52:14.347 DDgame[1457:433285] * 终止应用程序到期 未捕获的异常 'NSGenericException',原因:'* 集合 <__nsarraym:> 在枚举时发生了变异。 *** 第一次抛出调用堆栈:(0x18342259c 0x193b6c0e4 0x183421f50 0x187a57538 0x187a56c84 0x187a5661c 0x187a56510 0x10008f4c0 0x1000913b0 0x187a46bcc 0x187a43fd8 0x187a41038 0x187a6dfd8 0x100360a9c 0x187575280 0x187575118 0x1845718d0 0x1833c55e4 0x1833da200 0x1833da160 0x1833d80e0 0x1833050a4 0x18c49f5a4 0x187c36aa4 0x1000d1204 0x1000d1244 0x1941daa08) libc++abi.dylib: 以 NSException (lldb) 类型的未捕获异常终止
我使用了一个捕获所有异常断点,发现它在我的陀螺仪更新方法中崩溃了。有没有人有更安全的方法来使用更新按钮保持陀螺仪安全运行?
这里是 touchesBegan 方法的摘录,我已经删除了其他按钮中的代码,只是为了不在这里创建太多绒毛:(不确定你们是否需要这里的额外功能?)
else if node.name == "replayButton" {
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
NSNotificationCenter.defaultCenter().removeObserver(self, name: "pauseGame", object: nil)
let transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
scene.scaleMode = SKSceneScaleMode.AspectFill
self.view?.presentScene(scene, transition: transition)
}
我发现它在更新方法中的陀螺仪更新时崩溃了:
override func update(currentTime: NSTimeInterval) {
var timeSinceLast = currentTime - lastUpdateTimeInterval
lastUpdateTimeInterval = currentTime
if(!gameOver) {
//check for gameover
if (self.goldStash <= 0) {
gameOver = true
gameIsPaused = true
self.gameOverMethod()
}
//spawning logic
spawnEngine()
//gyro update
if (self.motionManager.gyroData != nil) {
self.updateGyroNodeWithData(self.childNodeWithName("GoldBack") as? SKSpriteNode, gyroIn: motionManager.gyroData)
self.updateGyroNodeWithDataReverse(self.childNodeWithName("GoldFront") as? SKSpriteNode, gyroIn: motionManager.gyroData)
self.updateGyroNode(self.childNodeWithName("CBack1") as? SKSpriteNode, gyroIn: motionManager.gyroData, minIn: cBack1position.x-10.0, maxIn: cBack1position.x+10.0, sensitivity: 0.8)
self.updateGyroNode(self.childNodeWithName("CBack2") as? SKSpriteNode, gyroIn: motionManager.gyroData, minIn: cBack2position.x-10.0, maxIn: cBack2position.x+10.0, sensitivity: 0.8)
self.updateGyroNode(self.childNodeWithName("CBack3") as? SKSpriteNode, gyroIn: motionManager.gyroData, minIn: cBack3position.x-10.0, maxIn: cBack3position.x+10.0, sensitivity: 0.8)
self.updateGyroNode(self.childNodeWithName("CBack4") as? SKSpriteNode, gyroIn: motionManager.gyroData, minIn: cBack4position.x-10.0, maxIn: cBack4position.x+10.0, sensitivity: 0.8)
}
}
if timeSinceLast > 1 {
timeSinceLast = kMinTimeInterval
}
updateWithTimeSinceLastUpdate(timeSinceLast)
}
func updateWithTimeSinceLastUpdate(timeSinceLast: NSTimeInterval) {
for dwarf in dwarves {
dwarf.updateWithTimeSinceLastUpdate(timeSinceLast)
}
}
【问题讨论】:
-
这些讨厌的错误通常发生在 GameScene 开始枚举包含仍在修改过程中的对象的数组时。尝试在 GameScene 更新方法中使用 BOOL 来指示在所述方法中运行代码之前何时完成所有初始设置操作。如果这不是问题,请仅在 GameScene 更新方法中修改 GameScene 更新方法中使用的数组。
标签: iphone swift crash sprite-kit nsarray