title: Cocos2d sprite with CCAnimation skipping when being moved only on device
body: <p>I wrote a demo application that works perfectly on the simulator, but when I put it on an actual device, the dice skip around. Here is a video as an example. After I restart the app, the animations are fine. The errors happened about 1 minute of repeatadly hitting the roll button.</p>
  
  <p><a href="http://youtu.be/N1k1QPa1brg" rel="nofollow">http://youtu.be/N1k1QPa1brg</a></p>
  
  <p>The code is all live at:</p>
  
  <p><a href="https://github.com/rnystrom/MartionDemo" rel="nofollow">https://github.com/rnystrom/MartionDemo</a>
  
  How I make the animation in the dice object:</p>
  
  <pre class="lang-c prettyprint-override"><code>CCAnimation *anim = [CCAnimation animationWithFrames:frames delay:delay];
  if(self.sprite){
      // Animate the sprite
      [self.sprite runAction:[CCAnimate actionWithAnimation:anim restoreOriginalFrame:NO]];
  }
  </code></pre>
  
  <p>The rolling function:</p>
  
  <pre class="lang-c prettyprint-override"><code>-(void)roll
  {
      // Array that contains the new positions of dice
      // Predetermine the position, check if that will be on top of other dice
      NSMutableArray* checkPos = [NSMutableArray array];
  
      for(Dice* d in rollDiceArray){
          [d resetPosition];    
  
          // Select a random position within bounds
          NSInteger x = arc4random() % 600 + 50;
          NSInteger y = arc4random() % 600 + 150;
          CGPoint location = CGPointMake(x, y);
  
          // Check if die will touch other dice
          while (! [self checkPositionWithPoint:location array:checkPos]) {
              // If position overlaps another die, get a new position
              // PROBLEM: This is O(infinity)!
              x = arc4random() % 600 + 50;
              y = arc4random() % 600 + 150;
              location = CGPointMake(x, y);
          }
  
          // If position does not overlap, add it to array of positions to be checked
          [checkPos addObject:[NSArray arrayWithObjects:[NSNumber numberWithInteger:x], [NSNumber numberWithInteger:y], nil]];
  
          // Animate the dice to a position
          // Addition in the switch is for some randomness and correcting the animation's timing offset
          NSInteger numberFrames;
          float frameRate;
          float mod = (float)(arc4random() % 60) / 100;
          switch (d.fileNum) {
              case 0:
                  numberFrames = kRayFrames;
                  frameRate = numberFrames/24 + mod;
                  break;
              case 1:
                  numberFrames = kRayFrames;
                  frameRate = numberFrames/24 + mod - 0.4;
                  break;
              case 2:
                  numberFrames = kTankFrames;
                  frameRate = numberFrames/24 + mod + 0.2;
                  break;
              case 3:
                  numberFrames = kChickenFrames;
                  frameRate = numberFrames/24 + mod;
                  break;
              case 4:
                  numberFrames = kCowFrames;
                  frameRate = numberFrames/24 + mod + 0.2;
                  break;
              case 5:
                  numberFrames = kHumanFrames;
                  frameRate = numberFrames/24;
                  break;
              default:
                  break;
          }
  
          id action = [CCMoveTo actionWithDuration:frameRate position:location];
          id ease = [CCEaseOut actionWithAction:action rate:4.0];
          [d.sprite runAction:ease];
      }
  }
  </code></pre>