Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Please take a look on my code and help me to find problem.

I used this tutorial: COCOS2D_ACCELEROMETER_MOVEMENT This works only sometime….some time not move..How can I resolve this problem? Here is my sample: DOWNLOAD Thanks for reading this…what's wrong with my code ? Is there any other way?

Here is my code:

#define kHeroMovementAction 1
#define kPlayerSpeed 500
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    // use the running scene to grab the appropriate game layer by it's tag

    // grab the player sprite from that layer using it's tag
    CCSprite *playerSprite = mPlayer;
    float destX, destY;
    BOOL shouldMove = NO;

    float currentX = playerSprite.position.x;
    float currentY = playerSprite.position.y;

    if(acceleration.x > 0.25) {  // tilting the device upwards
        destX = currentX - (acceleration.y * kPlayerSpeed);
        destY = currentY + (acceleration.x * kPlayerSpeed);
        shouldMove = YES;
    } else if (acceleration.x < -0.25) {  // tilting the device downwards
        destX = currentX - (acceleration.y * kPlayerSpeed);
        destY = currentY + (acceleration.x * kPlayerSpeed);
        shouldMove = YES;
    } else 

        if(acceleration.y < -0.25) {  // tilting the device to the right
        destX = currentX - (acceleration.y * kPlayerSpeed);
        destY = currentY + (acceleration.x * kPlayerSpeed);
        shouldMove = YES;
    } else if (acceleration.y > 0.25) {  // tilting the device to the left
        destX = currentX - (acceleration.y * kPlayerSpeed);
        destY = currentY + (acceleration.x * kPlayerSpeed);
        shouldMove = YES;
    } else {
        destX = currentX;
        destY = currentY;
    }

    if(shouldMove) 
    {
        CGSize wins = [[CCDirector sharedDirector] winSize];
        // ensure we aren't moving out of bounds     
        if(destX < 30 || destX > wins.width - 30 || destY < 30 || destY > wins.height - 100) {
            // do nothing
        } else {
            CCAction *action = [CCMoveTo actionWithDuration:0.5f position: CGPointMake(destX, playerSprite.position.y)];
            [playerSprite stopActionByTag:kHeroMovementAction];
            [action setTag:kHeroMovementAction];
            [playerSprite runAction:action];
        }
    } else {
        // should stop
        [playerSprite stopActionByTag:kHeroMovementAction];
    }

}
share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

That method of moving a player left and right is a little bit complicated and can be done in a much much nicer way using a simpler class design.

Create a player class and declare cgpoint vel and pos. You will also want a CCSprite in the header file of this class:

@interface player : CCLayer 
{
    CCSprite *spriteName;
    CGPoint vel;
    CGPoint pos;
}

@end

In the class set self.isAcelerometerEnabled = TRUE; in the init method.

Then in the player class you want:

-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{   
    float deceleration = 0.1f, sensitivity = 8.0f, maxVelocity = 150;

    // adjust velocity based on current accelerometer acceleration
    vel.x = vel.x * deceleration + acceleration.x * sensitivity;

    //limit the maximum velocity of the player sprite, in both directions (positive & negative values)
    vel.x = fmaxf(fminf(vel.x, maxVelocity), -maxVelocity);
}

In your update method for the player class you want:

-(void) update:(ccTime)delta
{
    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    float imageWidthHalved = [boat texture].contentSize.width * 0.25f;
    float leftBorderLimit = imageWidthHalved + 45;
    float rightBorderLimit = screenSize.width - imageWidthHalved - 45;

    pos = spriteName.position;
    pos.x += vel.x;

    if (pos.x < leftBorderLimit)
    {
        pos.x = leftBorderLimit;
        vel = CGPointZero;
    }
    else if (pos.x > rightBorderLimit)
    {
        pos.x = rightBorderLimit;
        vel = CGPointZero;
    }

    spriteName.position = pos;
}

Then in your main world class init just call (assuming you have declared a player object in your header file and called it player):

player = [player node];
[self addChild:player z:2];

Hopefully this will be enough code to get you started, assuming you know how to create classes in cocos2d. Also in your player class dont forget to call:

[self scheduleUpdate];

In the init method in your player class.

share|improve this answer
1  
Thanks a lot Lewis..this is what I need. Now working very smooth. –  Raj Aug 2 '12 at 4:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.