将带有addComponent()的多个GKAgent3D添加到GKComponentSystem时,该应用程序在self.agentSystem.update(deltaTime: delta)
行的renderer()函数中崩溃,由于某种原因,我可以在for循环中添加200,这很好,但是可以通过点击或一秒钟计时器,应用崩溃,并显示以下错误。
这可能是线程问题,还是更新开始运行后它们正在更改由GKComponentsSystem管理的数组?但是,拥有一个只能在停止时才能更新的系统似乎并不正确……特别是他们说的话应该在renderer()方法中运行。
我创建了一个测试项目,对Xcode入门项目几乎没有修改,可以在这里下载-> https://drive.google.com/open?id=1yEEi2KXrqnqpJtB7bpKNx6uNXELFHm7T
2019-01-28 20:58:08.030883 + 0700 AgentSystemTest [16197:4487626] 由于未捕获的异常“ NSGenericException”而终止应用程序, 原因:'* 集合<__ NSArrayM:0x2816fc5d0>在 被枚举。” ***第一个抛出调用堆栈:(0x1ca4ffea0 0x1c96d1a40 0x1ca4ff738 0x1e6d263cc 0x1023ed5ec 0x1023ed664 0x1de95de3c 0x1de960930 0x1de960f9c 0x1de9613e0 0x1dea12018 0x1de8a9e74 0x1de9d3cd0 0x1037fcdc8 0x10380b82c 0x1de9d3c40 0x1038d964c 0x1ce9c3854 0x1ca75fb94 0x1ca468ff8 0x1ca490844 0x1ca48ff90 0x1ca48ad48 0x1ca48a1cc 0x1cae7f404 0x1de8aa218 0x1de8aa34c 0x1ca11b2ac 0x1ca11b20c 0x1ca11ecf4)libc ++ abi.dylib:未捕获终止 NSException(lldb)*
类型的异常
for _ in 1...200
{
createPlayerAgent()
}
/*
// This will cause a the crash.
// But the loop of 200 above works just fine...
Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: {_ in
self.createPlayerAgent()
})
*/
func createPlayerAgent()
{
print("createPlayerAgent()")
let agent = GKAgent3D()
agent.delegate = self
agent.behavior = GKBehavior()
agentSystem.addComponent(agent)
}
@objc func renderer(_ renderer: SCNSceneRenderer,updateAtTime time: TimeInterval)
{
print("renderer")
// Calculate delta since last update and pass along to the agent system.
if (self.lastUpdateTime == 0) {
self.lastUpdateTime = time;
}
let delta = time - self.lastUpdateTime
self.lastUpdateTime = time;
print(delta)
self.agentSystem.update(deltaTime: delta)
}