Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

When you draw an object using spriteBatch.Draw(...) you have to select a color for the sprite itself.

We all know that the color is a mixture of "red, green and blue" values (or rgb). What not many people know is that, at least in XNA, there is a fourth value called alpha.

The alpha value indicates the transparency of your object, meaning that if you use it like the code below, your object will be half invisible (or half visible).

spriteBatch.Draw(..., Color.White * 0,5f,...);

You can play with that :)

Check a little more here, on the old XNA forums.

You need rate of fire (rof) property that you increase to some maxRof value when you hold down button. and when rof is equal to maxrof then you add bullet to a list of bullets (and reset rof to 0).

Do not make it invisible, remove it from list. Every instance of bullet should have "Active" property set to true on fire. (when you fire a bullet, add it to list) And when collsition happened set this property to false. pseudo code as example:

UPDATE

for each bullet in bullets
  -- update bullet position
  -- check collision if happened if yes then set Active to false
end for

bullets.removeall(function(c) NOT(c.active));

DRAW

for each bullet in bullets.findall(function(c) c.active)
  -- draw your bullets
end for
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top