views:

7

answers:

0

I have been working on this for days but cannot find a solution. Pretty much my acclerometer code is causing the moving image to get stuck whenever it hits the side of the screen. It causes the user to tilt the device more than needed to get it unstuck. Hopefully someone can help me. Square is the moving image

In the viewdidload method

AccelPoint.x = 160;
AccelPoint.y = 240;
XM = 160;
YM = 240;

Square.center = AccelPoint;

UIAccelerometer *Accelerometer = [UIAccelerometer sharedAccelerometer];
Accelerometer.delegate = self;
Accelerometer.updateInterval = 0.10f/5.0f;

In the outside void method...

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate: (UIAcceleration *)acceration {
if (acceration.x < 60) {
    XM += 1;
    AccelPoint.x = XM;
    Square.center = AccelPoint;
}
if (acceration.x > 60) {
    XM -= 1;
    AccelPoint.x = XM;
    Square.center = AccelPoint;
}
if (acceration.y < 0) {
    //60
    YM += 1;
    AccelPoint.y = YM;
    Square.center = AccelPoint;
}
if (acceration.y > 0) {
    //60
    YM -= 1;
    AccelPoint.y = YM;
    Square.center = AccelPoint;
}
X = acceration.x*50;
XM += X;
AccelPoint.x = XM;

//58 for both

Y = acceration.y*50;
YM-= Y;
AccelPoint.y = YM;

Square.center = AccelPoint;
//  if (AccelPoint.x < 0) {
//      AccelPoint.x = 0;
//  }
//50
if (AccelPoint.x < 25-(Square.frame.size.width / 2)) {
    AccelPoint.x = 25-(Square.frame.size.width / 2);
}
if (AccelPoint.x > 320) {
    AccelPoint.x = 320;
}
//305
Square.center = AccelPoint;
if (AccelPoint.y < 0) {
    AccelPoint.y = 0;
    //15
}
if (AccelPoint.y > 240) {
    AccelPoint.y = 240;
    //465
}
//if (AccelPoint.x < 320-(Square.frame.size.width / 2)) {
//  AccelPoint.x = 320-(Square.frame.size.width / 2);
//  self.XM = 320-(Square.frame.size.width / 2);
//}
Square.center = AccelPoint;

}