Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top