Hi,
I need some help with accelerometer coding. I have a sample code here which I retrieve from the net and would like to implement it into my project of racing game.
However, the sample code controls the image such that it starts at the center all the time(or even return to the center when it hits some ifelse statements).
My project requires the car to start at the corner of the phone, so is there any way I can change the coding to suit that?
Please help thx!!
#import "AccelerometerViewController.h"
@implementation AccelerometerViewController
@synthesize Object, NAME;
- (void)viewDidLoad {
[super viewDidLoad];
timer = [NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
[[UIAccelerometer sharedAccelerometer] setUpdateInterval

1.0 / kAccelerometerFrequency)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
position = CGPointMake(0,0);
}
// UIAccelerometerDelegate method, called when the device accelerates.
- (void)accelerometer

UIAccelerometer *)accelerometer didAccelerate

UIAcceleration *)acceleration {
// Update the accelerometer graph view
deviceTilt.x = acceleration.x;
deviceTilt.y = acceleration.y;
}
-(void)onTimer{
int speed;
speed = 5;
Object.center = CGPointMake(Object.center.x+position.x,Object.cent er.y+position.y);
//Moves the name to wherever the Object is.
NAME.center = CGPointMake(Object.center.x, 141);
//These two stop the Object from moving off the line.
if (Object.center.x > 444){Object.center = CGPointMake(444,160);}
if (Object.center.x < 36){Object.center = CGPointMake(36,160);}
if (deviceTilt.y > 0.3f) {position.x = -speed;}
else if (deviceTilt.y < -0.3f) {position.x = speed;}
else if (0.2f < deviceTilt.y > -0.2f){position.x = 0;}
// These just change the image when it gets offset from the center.
if (Object.center.x > 242){[Object setImage:[UIImage imageNamed:@"car_C.gif"] forState:UIControlStateNormal];}
if (Object.center.x < 500){[Object setImage:[UIImage imageNamed:@"car_C.gif"] forState:UIControlStateNormal];}