开发者

Calibrate Accelerometer?

开发者 https://www.devze.com 2023-04-05 08:27 出处:网络
I know this is a very general and vague question but I am curious how to get this functionality into my app in the easiest way possible. I see many games out there, Doodle Jump is my #1 example where

I know this is a very general and vague question but I am curious how to get this functionality into my app in the easiest way possible. I see many games out there, Doodle Jump is my #1 example where it allows you calibrate the accelerometer so if you开发者_StackOverflow中文版r sitting sideways the game will work good as new. How do they do this? I want the same functionality in my app but I am not sure what to do or where to go from here to get this functionality achieved.

Thanks!


You can consider "calibration" to be a constant reference to some root value or state. When you want to reset your calibrations, simply reset the root value from which all other values are derived. With the accelerometer, you have the luxury of knowing when a change is detected through a delegate callback, so all you need to do is write a method to "capture" the current state of the accelerometer.

In the tutorial outlined here, such a method is outlined as:

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
     calibration = acceleration.y;
}

But, this isn't as "controlled" as we'd like it to be. Instead, either break off your relation to UIAccelerometer as a delegate, or surround it with a BOOLean property indicating whether or not you'll allow calibration to occur. Something like this should work just fine:

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
     if (self.allowsCalibration)
         calibration = acceleration.y;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号