title: C++ Updating a pointer to a sprite's velocity
body: <p>I'm using a game engine supplied for the course I'm on (which also incorporates OpenGL) and I'm using an array (called basics) to store some enemy sprites. A new sprite is created after 80 frames (I'm sure there's a better way to do this than increment an integer) or rather a pointer to a sprite. In the below example, I want to set the enemy's velocity to move towards the player, except this only runs once because it sets the velocity once it's created, but will never set the same enemy's velocity again.</p>
  
  <pre><code>void CMyGame::OnUpdate() //60 frames
  {
      if (spawnTime &gt; 80) spawnTime = 0;
      spawnTime++;
      if (spawnTime == 80) 
      {
          CSprite* pBasicEnemy = new CSprite(rand() % 800, 575, basic_texture);
          basics.add( pBasicEnemy);
          basicEnemyVX = player.GetX() - pBasicEnemy-&gt;GetX();
          basicEnemyVY = player.GetY() - pBasicEnemy-&gt;GetY();
          pBasicEnemy-&gt;SetVelocity(basicEnemyVX, basicEnemyVY);
      }
  }
  </code></pre>
  
  <p>One thing I've tried is only adding the sprite to array inside the if statement instead of including everything. It runs but strangely, the velocity is not being updated every frame despite it now being local to the OnUpdate() function and not to an if statement.</p>
  
  <pre><code>void CMyGame::OnUpdate() //60 Frames
  {
      if (spawnTime &gt; 80) spawnTime = 0;
      spawnTime++;
      CSprite* pBasicEnemy = new CSprite(rand() % 800, 575, basic_texture);
      if (spawnTime == 80) 
      {
          basics.add( pBasicEnemy);
      }
      basicEnemyVX = player.GetX() - pBasicEnemy-&gt;GetX();
      basicEnemyVY = player.GetY() - pBasicEnemy-&gt;GetY();
      pBasicEnemy-&gt;SetVelocity(basicEnemyVX, basicEnemyVY);
  }
  </code></pre>
  
  <p>Any idea how to somehow update each enemy's velocity every frame?</p>