title: Accelerometer based movement
body: <p>Now woking on game that uses accelerometer to move player sprite left and right.
  I used this tutorial: <a href="http://www.cocos2d-iphone.org/wiki/doku.php/tips:using_accelerometer_for_sprite_movement" rel="nofollow noreferrer">COCOS2D_ACCELEROMETER_MOVEMENT</a>
  This works only sometime….some time not move..How can I resolve this problem?
  Here is my sample: <a href="http://www.4shared.com/zip/BUJJedIy/testAcc.html" rel="nofollow noreferrer">See this Sample</a>
  Thanks for reading this…what's wrong with my code ? Is there any other way?</p>
  
  <p>Here is my code:</p>
  
  <pre><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 &gt; 0.25) {  // tilting the device upwards
          destX = currentX - (acceleration.y * kPlayerSpeed);
          destY = currentY + (acceleration.x * kPlayerSpeed);
          shouldMove = YES;
      } else if (acceleration.x &lt; -0.25) {  // tilting the device downwards
          destX = currentX - (acceleration.y * kPlayerSpeed);
          destY = currentY + (acceleration.x * kPlayerSpeed);
          shouldMove = YES;
      } else 
  
          if(acceleration.y &lt; -0.25) {  // tilting the device to the right
          destX = currentX - (acceleration.y * kPlayerSpeed);
          destY = currentY + (acceleration.x * kPlayerSpeed);
          shouldMove = YES;
      } else if (acceleration.y &gt; 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 &lt; 30 || destX &gt; wins.width - 30 || destY &lt; 30 || destY &gt; 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];
      }
  
  }
  </code></pre>
  
  <p><strong>Updates: Here is <a href="https://gamedev.stackexchange.com/questions/33412/accelerometer-to-move-player-left-and-right/33427#33427">best way to do this.</a></strong></p>