Accelerometer occasionally doesn't work

Member
Posts: 161
Joined: 2005.07
Post: #1
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.

Code:
in the applicationDidFinishLaunching event:

[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
Quote this message in a reply
Member
Posts: 446
Joined: 2002.09
Post: #2
Setting the delagate to nil before changing the update frequency seems to help - so: setDelegate:nil, setUpdateInterval, then finally setDelegate:self.
Quote this message in a reply
Member
Posts: 269
Joined: 2005.04
Post: #3
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];
        }
    }
Quote this message in a reply
Member
Posts: 338
Joined: 2004.07
Post: #4
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
Quote this message in a reply
Member
Posts: 161
Joined: 2005.07
Post: #5
Awesome, thank you guys for the code samples, and thank you Justin for confirming that it works great!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Game play with the accelerometer Ropeburn 3 2,695 Sep 26, 2010 06:17 PM
Last Post: Ropeburn
  accelerometer settings baddapple 0 3,524 Apr 24, 2010 05:39 PM
Last Post: baddapple
  About ParticleSystem and Accelerometer ! Dorald 13 5,452 Apr 2, 2010 10:17 PM
Last Post: alerus
  How can I smooth accelerometer signal? riruilo 6 8,876 Feb 10, 2009 04:53 PM
Last Post: riruilo
  Accelerometer problem Parystec 4 3,329 Sep 30, 2008 01:28 PM
Last Post: kodex