I'm making a 2D Shooter game in XNA. I had been working on the shooting speed of the player (Every how often can the player shoot another bullet) and made it so that the player can only shoot again once the previous bullet has traveled a certain distance like so:
if (this.bulletList[0].BULLETS[(this.bulletList[0].BULLETS.Count) - 1].X >= Pos.X + attackSpeed)
canShoot = true;
Where bulletList is the available Projectiles the player can shoot, BULLETS is the List of Bullets already shot, and attackSpeed is the rate in which the bullets should be shot, or simply put: the distance the bullet has to travel until another bullet can be shot.
Now I've been working on Collisions. My method was to dispose of a bullet after it hits a target like so:
for (int i = 0; i < player.BULLETLIST[0].BULLETS.Count; i++)
{
if (CollisionManager.PlayerBulletOnBot(player.BULLETLIST[0].BULLETS[i], bot))
player.BULLETLIST[0].BULLETS.RemoveAt(i);
}
Problem is, if the bullet has been removed upon hitting a target, I can no longer ask if that bullet has passed the given distance for another bullet to be shot. To solve that, I'd like the bullet to turn invisible upon hit, and afterwards it'll be disposed of in another function that's already been made.
Simply have a Visible
flag for a Bullet
instance:
class Bullet {
public bool Visible { get; set; }
}
When it hits.. make it invisible:
// ... hit
bulletInstace.Visible = false;
Then check before drawing it:
if (bulletInstance.Visible)
drawBullet(bullet);
If it isn't visible, your drawing code should just skip over it.