Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6580771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:04:09+00:00 2026-05-25T16:04:09+00:00

After a google or two, I came up with this code to save the

  • 0

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. How 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:04:09+00:00Added an answer on May 25, 2026 at 4:04 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

after launching your app on google app engine. you can use the 'logs' page
I've spent my requisite two hours Googling this, and I can not find any
I've spent two days on this and I still can't find the cause of
Hi I have to set two address at a time in google map. User
I am now going to use the .jar file on http://code.google.com/p/google-api-translate-java/ . However, after
So, this seemed simple at first, but after crawling Google and here, the answer
Ordinarily I wouldn't just post an error message on SO, but after a Google
After test driving Google Chrome for 30 minutes or so, I like it, even
After a quick Google I couldn’t find anything too promising so I was wondering
I want my program always know all of the mountpoints. After a quick google

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.