gpt4 book ai didi

iphone - 枚举崩溃时,GyroUpdates和重新启动场景发生了变化(SpriteKit和Swift)

转载 作者:行者123 更新时间:2023-12-03 17:23:26 24 4
gpt4 key购买 nike

我有一个Swift SpriteKit场景,但并非每次都发生,但我通过按菜单对重启游戏按钮进行了一些测试,然后反复快速连续重启直到崩溃。它还会从加载场景的其他点随机崩溃。我的估计是,该行崩溃的可能性约为30%

class AppDelegate: UIResponder, UIApplicationDelegate {



与以下错误:

2015-03-07 09:52:14.347 DDgame[1457:433285] * Terminating app due to uncaught exception 'NSGenericException', reason: '* Collection <__NSArrayM: 0x170051e50> was mutated while being enumerated.' *** First throw call stack: (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: terminating with uncaught exception of type NSException (lldb)



我使用了一个catch all exception断点,发现它在陀螺仪更新的更新方法中崩溃。有没有人有更安全的方法来通过更新按钮使陀螺仪安全运行?

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

}

最佳答案

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

self.childNodeWithName("GoldBack") as? SKSpriteNode

它继续枚举节点的场景,然后销毁场景。我尝试放入一个 bool(boolean) 值以防止在打开菜单时运行陀螺仪更新,但是由于某种原因它不断崩溃,该百分比显着下降,但是没有任何作用。

最好的解决方案是在init中创建节点的变量,然后对update方法中的节点运行陀螺仪更新。
var goldBack = SKSpriteNode?()

goldBack = self.childNodeWithName("GoldBack") as? SKSpriteNode

然后在更新方法中:
self.updateGyroNodeWithData(goldBack, gyroIn: motionManager.gyroData)

我以前对此一无所知,但实际上对我有帮助的另一件事是在Xcode中添加了“所有异常”断点。 (这是通过转到断点导航器,按左下角的+并添加异常断点来完成的)

关于iphone - 枚举崩溃时,GyroUpdates和重新启动场景发生了变化(SpriteKit和Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28912821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com