0
\$\begingroup\$

I'm making a game using DirectXTK for my university project. When I press space bullet moves forward like I wanted, but when I let go it stops my bullet from moving. How do I keep the bullet moving when space was pressed?

void update
{
  //shooting
  if (kb.Space)
    Shoot(true);
  else
    Shoot(false);
}
void Render()
{
  if (draw)
  {
    for (int i = 0; i < 10; i++)
    {
      m_projectile[i]->Draw(m_bullet[i], m_view, m_proj, Colors::Red);
    }
  }
}
bool shoot(bool test)
{
  shoot = test;
  draw = true;

  //bool isShot = false;
  if (!shoot)
  {
    for (int i = 0; i < 10; i++)
    {
      m_bullet[i] = Matrix::CreateRotationY(m_ship._42) * m_ship;
    }
  }
  else
  {
    m_bullet[0] = Matrix::CreateTranslation(-Vector3::Forward *float(1))*Matrix::CreateRotationY(0.f) * m_bullet[0];
    //m_bullet *= Matrix::CreateTranslation(-Vector3::Forward);
  }

  return draw;
}
\$\endgroup\$
  • 1
    \$\begingroup\$ It sounds like the code to move the bullet is in a function tied to the press-and-hold of the space-bar. Perhaps consider moving this code so that it runs after the space-bar is pressed rather than held down...? \$\endgroup\$ – Neil Hibbert Mar 16 '16 at 12:07
3
\$\begingroup\$

You've joined together the creation of the bullet when pressing the space bar to the movement of the bullet. You should change your code so that when the space bar is pressed, a bullet is created. Separately, still in your update method, apply your translation based on velocity to any bullets that exist.

You'll also want to remove bullets at some point, likely either some time after they were created, or when they pass outside the bounds of your game - or better yet, on collision with something.

\$\endgroup\$
  • \$\begingroup\$ Okay, I got that working, but only on one bullet. with using unique_ptr how do I create a new object? i cant get the examples from the internet to work \$\endgroup\$ – Przemek Hendel Mar 16 '16 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.