0

I am writing a game on iOS and I am trying to setup the response movement using the accelerometer. So when I start the game on the iPhone, everything is fine and accelerometer is responding without a problem. But when I stop the game loop (pause menu), and then start it again, the entity on screen jumps somewhere, and then after a couple of seconds comes back into place. I believe this is happening due to a difference in delta between the game loop and the delta of accelerometer. So I was wondering is there any way to set the accelerometer on iPhone to only update values when the game loop updates?

Values from UIAccelerometer are read like this (I just need the x-axis) - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { accelerometValues[0] = acceleration.x * 0.1f + accelerometValues[0] * (1.0 - 0.1f); }

The accelerometer is setup as follows [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0 / 60.0]; [[UIAccelerometer sharedAccelerometer] setDelegate:currentScene];

The game loop itself was taken from tutorial by Alex Diener GameLoop Tutorial

Setrino
  • 341
  • 4
  • 9

1 Answers1

0

When you pause, you can stop listening for Accelerometer events by removing the delegate:

[[UIAccelerometer sharedAccelerometer] setDelegate:nil];

Then just set up the accelerometer again when the game resumes. There shouldn't be a need to alter the data that is returned, just ignore any data you don't need.

Kekoa
  • 27,892
  • 14
  • 72
  • 91
  • Thank you, that worked. I didn't want to alter any data but it seemed as if it was cached and then released. – Setrino Dec 30 '12 at 14:01
  • If your game loop is in a different thread and that thread's run loop was not running, then it could queue up those method calls from the accelerometer, since the accelerometer was still calling the delegate when the game was paused. That could be what you were seeing. When the thread is resumed, all those method calls would be executed. – Kekoa Jan 02 '13 at 22:03
  • That sounds reasonable. Thank you once again. – Setrino Jan 02 '13 at 22:16