title: Animating sprites at random rates
body: <p>I have a program that displays 5 explosions in a line at the same time. I would like to know how to get each explosion to animate at different rates. Here is the code that actually animates the sprites:</p>
  
  <pre><code>//=========================================================================
  //
  // Helper functions
  //
  //========================================================================
  void Sprite_Draw_Frame(LPDIRECT3DTEXTURE9 texture, int destx, int desty, int framenum, int framew, int frameh, int columns)
  {
      D3DXVECTOR3 position( (float)destx, (float)desty, 0);
      D3DCOLOR white = D3DCOLOR_XRGB(255, 255, 255);`
  
      RECT rect;
      rect.left = (framenum % columns) * framew;
      rect.top = (framenum / columns) * frameh;
      rect.right = rect.left + framew;
      rect.bottom = rect.top + frameh;
  
      spriteobj-&gt;Draw(texture, &amp;rect, NULL, &amp;position, white);
  }
  
  void Sprite_Animate(int &amp;frame, int startframe, int endframe, int direction, int &amp;starttime, int delay)
  {
      if((int)GetTickCount() &gt; starttime + delay)
      {
          starttime = GetTickCount();
  
          frame += direction;
          if(frame &gt; endframe) frame = startframe;
          if(frame &lt; startframe) frame = endframe;
      }
  }
  
  //============================================================================
  //
  // Function calls
  //
  //==============================================================================
  </code></pre>
  
  <p>Tried to use random numbers for the delay variable in <code>Sprite_Animate</code> so that the frames would be delayed at different rates. However, the explosions continued to animate in sync.</p>
  
  <p>The <code>Sprite_Animate</code> function just updates the global variables <code>frame</code> and <code>starttime</code> to continue drawing each new frame with the <code>Sprite_Draw_Frame</code> function.</p>
  
  <pre><code>    //animate and draw the sprite
      for(int i = 0; i &lt; 5; i++)
      {
          Sprite_Animate(frame, 0, 29, 1, starttime, rand() %100);
          Sprite_Draw_Frame(explosion, (i * 100), 100, frame, 128, 128, 6);
      }
  </code></pre>
  
  <p>There is a simple solution to this, and if anyone wants the full code I can send it to you by request.</p>