Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);
share|improve this question

closed as off-topic by Josh Petrie Dec 16 '14 at 2:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Josh Petrie
If this question can be reworded to fit the rules in the help center, please edit the question.

"Softwares"? What an interesting neologism, I really like it. ;) – jcora 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. – jcora Apr 29 '12 at 22:50
Done! Thanks! Have a nice day. ;) – Lucas Moreira Apr 29 '12 at 23:10