Hello,
Right now, I am trying to make my sprite move left when the device is tilting left and move right when the device is tilted right. There is no Y axis movement at all.(Which is good) The code I am using seems to be working just fine, except that when tilted left, the sprite only moves about a 3rd of the way across the screen instead of to the other side like I want it to. Do you have any suggestions on how to make it work? I would like to keep it strictly to a cocos2d application. No chipmunk or box2D. This is portrait mode.
Here is the code:
(void) accelerometer:(UIAccelerometer )accelerometer didAccelerate:(UIAcceleration)acceleration {
float destX; float destY;
BOOL shouldMove = NO;
if (acceleration.x > 0) { destX = acceleration.x * kPlayerSpeed; shouldMove = YES; } else if (acceleration.x < 0) { destX = acceleration.x * kPlayerSpeed; shouldMove = YES; }
if(shouldMove) {
CCAction *action = [CCMoveTo actionWithDuration:1 position:CGPointMake(destX, 40)]; [action setTag:kHeroMovementAction]; [player runAction:action]; } else { // should stop [player stopActionByTag:kHeroMovementAction];
}
}