【问题标题】:Stop gyro updates停止陀螺仪更新
【发布时间】:2013-04-10 11:56:45
【问题描述】:

我无法在我的 Cocos2D 游戏中停止 Gyro 更新,

我的 init 中有以下代码:` //gyro motionManager = [[CMMotionManager alloc] init]; 参考态度=无;

    [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 语句中的代码不断被调用。

【问题讨论】:

    标签: ios xcode cocos2d-iphone core-motion


    【解决方案1】:

    我通过以下代码找到了解决方案:`

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

    【讨论】:

      【解决方案2】:

      就像rickster说的,你需要在CMMotionManager实例上调用stopGyroUpdates。所以你需要在接口实现中创建一个实例变量或属性。

      在您声明接口的 .m 文件中:

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

      然后根据需要进行初始化

      motionManager = [[CMMotionManager alloc] init]; 
      

      然后当你需要停止更新时你调用

      [self.motionManager stopGyroUpdates]
      

      【讨论】:

        【解决方案3】:

        您需要在您的 CMMotionManager 实例上调用 stopGyroUpdates,而不是(或除了)self。 (这意味着如果您调用 startGyroUpdates 的方法(例如,在属性或 ivar 中)还没有,则该实例需要持续超出范围。)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-10-16
          • 1970-01-01
          相关资源
          最近更新 更多