Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-02-2009, 08:06 PM   #1 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 5
silenceter is on a distinguished road
Default Help !

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] setUpdateInterval1.0 / kAccelerometerFrequency)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];

position = CGPointMake(0,0);
}

// UIAccelerometerDelegate method, called when the device accelerates.
- (void)accelerometerUIAccelerometer *)accelerometer didAccelerateUIAcceleration *)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];}
silenceter is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 462
11 members and 451 guests
Domele, Duncan C, Feldspar, MacBook MH, Objective Zero, peterwilli, pipposanta, Punkjumper, SLIC, taylor202, Today's Posts
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,694
Threads: 94,137
Posts: 402,950
Top Poster: BrianSlick (7,990)
Welcome to our newest member, peterwilli
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:05 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0