2013-04-10 78 views
0

我不能工作了停在我的Cocos2D遊戲陀螺更新,停止陀螺更新

我已經得到了我的init下面的代碼:`//陀螺 MotionManager中= [[CMMotionManager的alloc]初始化] referenceAttitude = nil;

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

然後在陀螺儀進行自我更新我檢查時進度超過100%if (progress > 100) { [self pauseSchedulerAndActions]; [self stopGyroUpdates];

然後:

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

}

,但會將檢查...所以if語句中的代碼不斷被調用。

回答

0

像rickster說的,您需要調用stopMyroUpdates在CMMotionManager實例上。因此,您需要在接口實現中創建實例變量或屬性。

在您.m文件在您的聲明接口:根據您的需要

motionManager = [[CMMotionManager alloc] init]; 

然後,當你需要停止你打電話

[self.motionManager stopGyroUpdates] 
0

您需要在您的CMMotionManager實例上調用stopGyroUpdates,而不是(或除此之外)自行調用。 (這意味着如果您調用startGyroUpdates的方法(例如,在屬性或ivar中),實例需要持續超出範圍。)

0
更新

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

對其進行初始化

我用下面的代碼找到解決方案:'

-(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!"); 


}` 

和陀螺儀更新本身:

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]; 


    }