Hey guys, I'm creating a little game using C++ and SFML. Ran into a little problem trying to make my tank shoot more than one bullet. Hope you guys can give me some help. I've attached a small snippet of code that recreates the problem.
Loop to shoot
if(Game::GetInput().IsKeyDown(sf::Key::Space))
{
PrimaryWeapon PW1;
sf::Vector2f pos = PlayerTank::GetPosition();
if(!Game::GetGameObjectManager().Get("SmallBullet"))
{
PW1.Fire(pos);
}
}
void PrimaryWeapon::Fire(sf::Vector2f pos)
{
SmallBullet* i = new SmallBullet;
i->SetPosition(pos.x, pos.y);
Game::GetGameObjectManager().Add( "SmallBullet", i);
}
void SmallBullet::Update(float elapsedTime)
{
GetSprite().Move(Velocity * elapsedTime, 0);
sf::Vector2f pos = GetPosition();
if (pos.x >= Game::SCREEN_WIDTH)
{
Game::GetGameObjectManager().Remove( "SmallBullet");
}
}
the GameObjectManager in the code is a std::map that automatically draws and updates all items added to it, in the game loop. Remove() deletes the pointer to the object and then entry in the map.
Edit:: the code is supposed to cause the bullet to be deleted if it hits the side of the screen. Once it is deleted, a new bullet can be created and "shot" again. In my program however, once it hits the side of the screen, it disappears and no new bullets are created when spacebar is pressed.