我正在使用精灵套件进行游戏。在我的游戏中,我使用加速度计通过倾斜设备将节点一侧移动到另一侧。下面的代码运行平稳,但在8-10次重新启动游戏后,对倾斜的反应有点滞后。然后我关闭应用程序,问题自行重复。SpriteKit加速度计问题
我在didMove(to view: SKView)
motionManger.startAccelerometerUpdates()
,并有一个看起来功能像这样
func processUserMotion(forUpdate currentTime: CFTimeInterval) {
Ball.position.x += xAcceleration * 50 // xAcceleration is a variable that is a CGFloat = 0
motionManger.accelerometerUpdateInterval = 0.1
motionManger.startAccelerometerUpdates(to: OperationQueue.current!) { (data:CMAccelerometerData?, error:Error?) in
if let accelerometerData = data {
let acceleration = accelerometerData.acceleration
self.xAcceleration = CGFloat(acceleration.x) * 0.75 + self.xAcceleration * 0.25
}
}
}
override func update(_ currentTime: TimeInterval) {
processUserMotion(forUpdate: currentTime)
}
任何想法,为什么这种情况正在发生或有更好的方式,使节点响应以及当装置被倾斜到左,右?谢谢!
更新:
我终于明白为什么accelorometer滞后。我所要做的只是将motionManger.stopAccelerometerUpdates()
添加到我的GameOver场景中。这样做后,我会回到GameScene 30加倍的时间,并且它反应良好!希望这个小小的错误能够帮助未来的人!
你检查现场被释放?如果没有,请将'deinit {print“deinit”}'添加到场景类并转换到新场景。 – 0x141E
是的,我没有检查,它正在被释放。我发现我的解决方案。菜鸟错误!在我的游戏转移功能中,我忘了添加motionManger.stopAccelerometerUpdates()。 – sicvayne