# Cocos2D and iPhone's accelerometer

good afternoon!

Today I've started to develop a game and I got a trouble. So, first at all I would like to say that I have no experience developing games...

So, I've a CCSprite in the layer and I want to move this between the scene with a margin (top, bottom, left and right).

The minimum X should be -0.8 (in X-axis using the accelerometer) and the maximum 8. The minimun Y should be -1 (in Y-axis) or 0 in Z-axis and the maximum 0 (in Y-axis) or -1 (in Z-axis).

Ok, knowing this, I've tried to do this using the range between minimum and maximum space and accelerometer value but I haven't getting this working vertically (Y-axis on the iPhone). Then I got a done code in the web but this don't works when I want to move the object to back (the object only goes).

Did someone know how to fix this?

My actual code:

``````- (void)updatePosition:(ccTime)dt {
CGSize winSize = [[CCDirector sharedDirector] winSize];

float maxY = winSize.height - car.contentSize.height/2 - 20;
float minY = car.contentSize.height/2 + 20;
float newY = car.position.y + (carPointsPerSecY * dt);
newY = MIN(MAX(newY, minY), maxY);

float maxX = winSize.width - car.contentSize.width/2 - 40;
float minX = car.contentSize.width/2 + 40;
float newX = car.position.x + (carPointsPerSecX * dt);
newX = MIN(MAX(newX, minX), maxX);

car.position = ccp(newX, newY);
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
float speedY = acceleration.y * 500;
if(carPointsPerSecY < speedY) {
carPointsPerSecY = MIN(carPointsPerSecY + 10, speedY);
} else {
carPointsPerSecY = MAX(carPointsPerSecY - 2, speedY);
}

float speedX = acceleration.x * 200;
if(carPointsPerSecX < speedX) {
carPointsPerSecX = MIN(carPointsPerSecX + 10, speedX);
} else {
carPointsPerSecX = MAX(carPointsPerSecX - 10, speedX);
}

NSLog(@"Acceleration: X (%f) - Y (%f) - Z (%f)", acceleration.x, acceleration.y, acceleration.z);
}
``````
-
"Softwares"? What an interesting neologism, I really like it. ;) –  jco Apr 29 '12 at 21:14
I think you know what I mean :P By the way, do you know how to do that? –  Lucas Moreira Apr 29 '12 at 22:16
Of course I do, but it's kinda cool to say "softwares", just like "I can haz" or "teh" or "internets"... And no, sorry, I have no experience on that field. The only thing I can advise you to do, is ask this on Stack Overflow. This question seems only contextually related to gamedev, but the problem is purely of programming, technical nature, or so it seems, and therefor is best fitted for SO, not GD. –  jco Apr 29 '12 at 22:50
Done! Thanks! Have a nice day. ;) –  Lucas Moreira Apr 29 '12 at 23:10