Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone GyroUpdate和重新启动枚举崩溃时发生突变的场景(SpriteKit和Swift)_Iphone_Swift_Crash_Sprite Kit_Nsarray - Fatal编程技术网

Iphone GyroUpdate和重新启动枚举崩溃时发生突变的场景(SpriteKit和Swift)

Iphone GyroUpdate和重新启动枚举崩溃时发生突变的场景(SpriteKit和Swift),iphone,swift,crash,sprite-kit,nsarray,Iphone,Swift,Crash,Sprite Kit,Nsarray,我有一个Swift SpriteKit场景,并不是每次都会发生,但我在重新启动游戏按钮上做了一些测试,按下菜单,然后快速连续地重新启动,直到崩溃。它还从加载场景的其他点随机崩溃。据我估计,在这条线路上,它崩溃的可能性约为30% 类AppDelegate:UIResponder、UIApplicationLegate{ 出现以下错误: 2015-03-07 09:52:14.347 DDgame[1457:433285]*终止应用程序到期 要取消捕获异常“NSGenericeException”,

我有一个Swift SpriteKit场景,并不是每次都会发生,但我在重新启动游戏按钮上做了一些测试,按下菜单,然后快速连续地重新启动,直到崩溃。它还从加载场景的其他点随机崩溃。据我估计,在这条线路上,它崩溃的可能性约为30%

类AppDelegate:UIResponder、UIApplicationLegate{

出现以下错误:

2015-03-07 09:52:14.347 DDgame[1457:433285]*终止应用程序到期 要取消捕获异常“NSGenericeException”,原因:'*集合 在被枚举时发生了变异。” ***第一次抛出调用堆栈:(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)类型的未捕获异常终止

我使用了一个捕获所有异常断点,发现它在gyro更新的更新方法中崩溃了。有没有人有更安全的方法让gyro通过更新按钮安全运行

这里是touchesbearth方法的摘录,我删除了其他按钮中的代码,只是为了避免在这里产生太多的绒毛:(不确定你们是否需要额外的东西?)

我发现它在更新方法中的陀螺仪更新上崩溃:

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)
    }

}
self.updateGyroNodeWithData(goldBack, gyroIn: motionManager.gyroData)
覆盖函数更新(当前时间:NSTimeInterval){
var timeSinceLast=currentTime-lastUpdateTimeInterval
lastUpdateTimeInterval=currentTime
如果(!gameOver){
//检查游戏结束
如果(自我)黄金储备1{
timeSinceLast=kMinTimeInterval
}
updateWithTimeSinceLastUpdate(timeSinceLast)
}
func updateWithTimeSinceLastUpdate(timeSinceLast:NSTimeInterval){
矮人中的矮人{
dwarf.updateWithTimeSinceLastUpdate(timeSinceLast)
}
}

我解决了这个问题。它就在这段代码中:

self.childNodeWithName("GoldBack") as? SKSpriteNode
当场景被破坏时,它一直在为节点枚举场景。我尝试在菜单打开时输入布尔值以防止gyro更新运行,但由于某种原因,它一直崩溃,百分比显著下降,但有些东西不起作用

最好的解决方案是在init中创建节点变量,然后在update方法中运行gyro更新

var goldBack = SKSpriteNode?()

goldBack = self.childNodeWithName("GoldBack") as? SKSpriteNode
然后在更新方法中:

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)
    }

}
self.updateGyroNodeWithData(goldBack, gyroIn: motionManager.gyroData)

我以前不知道这一点,但另一件真正帮助我的事情是在Xcode中添加“所有异常”断点。(这是通过转到断点导航器,按下左下角的+并添加异常断点来完成的)

当游戏场景开始枚举包含仍在修改过程中的对象的数组时,通常会发生这些令人讨厌的错误。请尝试在游戏场景更新方法中使用布尔值,以指示在运行所述方法中的代码之前,所有初始设置操作何时完成。如果不是问题,请仅修改t中使用的数组他在游戏场景更新方法中使用了游戏场景更新方法。