How can i shoot a vector of bullets?

HEy i am wondering if anyone can help me shoot my bullets.
I have a bullet class and player class, can i have some guidance as to where to declare my object and vector, and how to use my for loop to add another element to the vector and call my shoot function.

thankyou very much in advance!
do you have any code? I'd recommend keep a constant max number of bullets and reinitialize the bullet each time its shot.
You'll need some third class to handle the game itself. This would contain both the player object and the vector of bullets.

To fire a new bullet, you would do something like this in one of your game member functions:

bullets.push_back(Bullet(player.x, player.y, player.direction));

Obviously this depends on how you implement Bullet and Player.

When it comes time to move the bullets, do something like this:

1
2
for(std::size_t i = 0; i < bullets.size(); ++i)
    bullets[i].move();


Eventually, some of these bullets will hit an object, leave the screen, or otherwise need to be removed. When that happens, do this:

1
2
std::vector <Bullet>::iterator it = bullets.find(Bullet(/*arguments representing the bullet being removed*/));
bullets.erase(it);
Topic archived. No new replies allowed.