After a google or two, I came up with this code to save the current position the user is in and save it to NSUserDefaults. This is in my settings view: Code:
//Add code for calibrating accelerometer
UIAcceleration *acceleration;
float accelX = (acceleration.x - [[NSUserDefaults standardUserDefaults] floatForKey:@"X-Calibrate"]);
float accelY = (acceleration.y - [[NSUserDefaults standardUserDefaults] floatForKey:@"Y-Calibrate"]);
[[NSUserDefaults standardUserDefaults] setFloat:accelX forKey:@"X-Calibrate"];
[[NSUserDefaults standardUserDefaults] setFloat:accelY forKey:@"Y-Calibrate"];
To reset the calibration I do: Code:
[[NSUserDefaults standardUserDefaults] setFloat:0 forKey:@"X-Calibrate"];
[[NSUserDefaults standardUserDefaults] setFloat:0 forKey:@"Y-Calibrate"];
Now here is where I need a slight bit of help. Now that I have these values in NSUserDefaults. How would I apply it to the following code so that the calibration is 'fully' implemented? Here is the code for the accelerometer currently: Code:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceration {
AccelPoint.x += (acceration.x*50);
if (AccelPoint.x < 0) {
AccelPoint.x = 320;
}
if (AccelPoint.x > 320) {
AccelPoint.x = 0;
}
playerSpeed += 0.066;
AccelPoint.y += playerSpeed;
Square.center = AccelPoint;
}
This is some of the variables and how 开发者_如何学JAVAthey are defined: Code:
CGPoint AccelPoint;
float playerSpeed;
Thanks!
Also quick little site question for that accelerometer code, should I be using if-elseif? Would that be more efficient or should I leave it at if-if?
精彩评论