开发者

UIAccelerometer help?

开发者 https://www.devze.com 2023-04-05 23:10 出处:网络
After a google or two, I came up with this code to save the current position the user is in (so I can calibrate it) and save it to NSUserDefaults. This is in my settings view:

After a google or two, I came up with this code to save the current position the user is in (so I can calibrate it) and save it to NSUserDefaults. This is in my settings view:

//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:

[[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. H开发者_StackOverflow中文版ow would I apply it to the following code so that the calibration is 'fully' implemented? Here is the code for the accelerometer currently:

- (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 they are defined:

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?

Edit1:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceration {
    float accelX = (acceration.x - [[NSUserDefaults standardUserDefaults] floatForKey:@"X-Calibrate"]);
    float accelY = (acceration.y - [[NSUserDefaults standardUserDefaults] floatForKey:@"Y-Calibrate"]);
    AccelPoint.x += (accelX*50);
    if (AccelPoint.x < 0) {
        AccelPoint.x = 320;
    }
    if (AccelPoint.x > 320) {
        AccelPoint.x = 0;
    }
    playerSpeed += 0.066;

    AccelPoint.y += playerSpeed;

    Square.center = AccelPoint;

}

In my code, I am not using accelY. How should I apply that to my code? I cleaned up the code a bit to make it a bit clearer.


Not sure exactly how you want use the data you're storing, but to access it it's just:

float xCal = [[NSUserDefaults standardUserDefaults] floatForKey:@"X-Calibrate"];
float yCal = [[NSUserDefaults standardUserDefaults] floatForKey:@"Y-Calibrate"];

EDIT:

So based on your comment, maybe try something like this.

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *) acceration {
    float accelX = (acceration.x - xCal);
    float accelY = (acceration.y - yCal);
    AccelPoint.x += (accelX*50);
    if (AccelPoint.x < 0) {
        AccelPoint.x = 320;
    }
    if (AccelPoint.x > 320) {
        AccelPoint.x = 0;
    }
    playerSpeed += 0.066;
    AccelPoint.y += playerSpeed;
    Square.center = AccelPoint; 
}
0

精彩评论

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

关注公众号