I am making a game using sprite-kit. In my game I am using the accelerometer to move the node side to side by tilting the device. The following code works smoothly but after 8-10 game restarts, it kind of lags a bit to respond to the tilt. I then close the application and the problem repeats it self.
I have motionManger.startAccelerometerUpdates()
in didMove(to view: SKView)
and have a function that looks like so
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)
}
Any idea why this is happening or a better way to make the node respond well when the device is being tilted to left and right? Thanks!
UPDATE:
I finally figured out why the accelorometer was lagging. All I had to do was add motionManger.stopAccelerometerUpdates()
to my GameOver scene. After doing so, I would transition back to GameScene 30 plus times and it responds well! Hope this little error helps someone in the future!
deinit{print"deinit"}
to your scene class and transition to a new scene. – 0x141E