Search code examples
iosxcodecocos2d-iphonecore-motion

Stop gyro updates


I cant work out to stop the Gyro updates in my Cocos2D game,

I have got the following code in my init: ` //gyro motionManager = [[CMMotionManager alloc] init]; referenceAttitude = nil;

    [motionManager startGyroUpdates];
    timer = [NSTimer scheduledTimerWithTimeInterval:0.2
                                             target:self
                                           selector:@selector(doGyroUpdate)
                                           userInfo:nil
                                            repeats:YES];

Then in the gyro update itself I check when the progress is more than 100% if (progress > 100) { [self pauseSchedulerAndActions]; [self stopGyroUpdates];

Then:

- (void)stopGyroUpdates{
NSLog(@"Stop gyro update");

}

but it keeps checking... So the code within the if statement keeps getting called.


Solution

  • Like rickster said, you need to call stopGyroUpdates on the CMMotionManager instance.so you need to create an instance variable or property in the interface implementation.

    In your .m file where your declare the interface:

    @interface ViewController ()
    @property (strong, nonatomic) CMMotionManager  *motionManager;
    @end
    

    Then initialize it as you require

    motionManager = [[CMMotionManager alloc] init]; 
    

    Then when you need to stop updates you call

    [self.motionManager stopGyroUpdates]