Accelerometer occasionally doesn't work
Every once in a while, I'll launch the game I'm working on (either from the iPod itself or through Xcode) and the accelerometer won't work at all. If I quit and launch the app a second time, it works fine. I have a feeling it's related to some power-down mode on the iPod since I've only ever seen it happen when my iPod has been locked for some time and I unlock it and immediately launch the game.
Is there some way to avoid this? I never had this problem when I was playing Super Monkey Ball on my iPod so maybe I'm missing something.
Is there some way to avoid this? I never had this problem when I was playing Super Monkey Ball on my iPod so maybe I'm missing something.
Code:
in the applicationDidFinishLaunching event:
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
Setting the delagate to nil before changing the update frequency seems to help - so: setDelegate:nil, setUpdateInterval, then finally setDelegate:self.
Frank C. Wrote:Setting the delagate to nil before changing the update frequency seems to help - so: setDelegate:nil, setUpdateInterval, then finally setDelegate:self.
This, plus in your update loop you may want to check to make sure you're actually receiving data, and reset the accelerometer if you're not. Something like this with "accelerationReceived" set to NO in your program init, and set to YES inside your "accelerometer:didAccelerate:" method.
Code:
// Check the accelerometer counter to make sure it's working...
if (accelerationReceived == NO)
{
++accelerationCounter;
if (accelerationCounter >= 30)
{
accelerationCounter = 0;
[[UIAccelerometer sharedAccelerometer] setDelegate:nil];
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0f / 30.0f)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
}
}Bachus Wrote:This, plus in your update loop you may want to check to make sure you're actually receiving data, and reset the accelerometer if you're not. Something like this with "accelerationReceived" set to NO in your program init, and set to YES inside your "accelerometer:didAccelerate:" method.
Seconding this. We do this in every one of our games that requires the accelerometer, and it's worked wonderfully.
Justin Ficarrotta
http://www.justinfic.com
"It is better to be The Man than to work for The Man." - Alexander Seropian
Awesome, thank you guys for the code samples, and thank you Justin for confirming that it works great!
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Game play with the accelerometer | Ropeburn | 3 | 2,352 |
Sep 26, 2010 06:17 PM Last Post: Ropeburn |
|
| accelerometer settings | baddapple | 0 | 2,995 |
Apr 24, 2010 05:39 PM Last Post: baddapple |
|
| About ParticleSystem and Accelerometer ! | Dorald | 13 | 4,814 |
Apr 2, 2010 10:17 PM Last Post: alerus |
|
| How can I smooth accelerometer signal? | riruilo | 6 | 7,123 |
Feb 10, 2009 04:53 PM Last Post: riruilo |
|
| Accelerometer problem | Parystec | 4 | 3,011 |
Sep 30, 2008 01:28 PM Last Post: kodex |
|

