Calibrate Accelerometer?
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 BOOL
ean 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;
}
精彩评论