Question

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.

Was it helpful?

Solution 2

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]

OTHER TIPS

I found the solution with the following code: `

 -(void) callgyro:(int)gyroprocess {


NSLog(@"%i", gyroprocess);

if (gyroprocess < 100 ) {
    motionManager = [[CMMotionManager alloc] init];
    referenceAttitude = nil;
    [motionManager startGyroUpdates];
    timertwee = [NSTimer scheduledTimerWithTimeInterval:0.2
                                             target:self
                                           selector:@selector(doGyroUpdate)

                                           userInfo:nil
                                            repeats:YES];
}
else {
    [motionManager stopGyroUpdates];
    [timertwee invalidate];

    NSLog(@"Gyro moet stoppen!");


}`

and within the gyro update itself:

 if (progress > 100) {

        [self callgyro:progress];

        [self.timer invalidate];
        self.timer = nil;
        [Blikje setImage:[UIImage imageNamed:@"pop3.png"]];
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        [prefs setFloat:numhundreds forKey:@"score"];
        progress = 0;

        [self stopGyroUpdates];


    }

You need to call stopGyroUpdates on your instance of CMMotionManager, not (or in addition to) on self. (This means that instance needs to persist beyond the scope if the method in which you call startGyroUpdates -- say, in a property or ivar -- if it doesn't already.)

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