简体   繁体   中英

Shoot multiple bullets

I'm making a little variant of space invaders in XNA. I'm at the point where I must make my ship shoot. It shots correctly, but when I shoot again, it redraws the shot, making the last one disappear. Any idea on how to solve this? (I'd like my shots not to disappear if there's more than one on the screen)

Here's my code to create the shot:

if (_keyboardState.IsKeyDown(Keys.Up))
{
   _ShotDownPosition.X = _ShipDownPosition.X + (_ShipDown.Width / 2) - (_ShotDown.Width/2);
   _ShotDownPosition.Y = _ShipDownPosition.Y - (_ShotDown.Height/2);
   shotDown = true;
}

And here's my code for moving the shot:

if (shotDown == true)
{
   _ShotDownDisplacement = _DisplacementUp;
}
_ShotDownPosition += _ShotDownDisplacement * _ShotSpeed * (float)gameTime.ElapsedGameTime.TotalMilliseconds;

knowing that:

_DisplacementRight = new Vector2(1, 0);
_DisplacementLeft = new Vector2(-1, 0);
_DisplacementUp = new Vector2 (0, -1);
_DisplacementDown = new Vector2(0, 1);

It seems that you manage the shot only with a Vector2 _ShotDownPosition and that's wrong. You need a List<Vector2> of bullets, or maybe create a class Bullet , or whatever, in order to create a new object for each bullet, as Ondrej commented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM