Dustin Liu
Dustin Liu

Reputation: 1

C# XNA Space Invaders. Tank bullet not showing

The bullets in my code seem to be shooting the invaders, since the invaders disappear. However there is no bullet coming out of the tank and hitting the invaders. I don't know where the bullet went.

Initialize Code for bullet:

recBullet = new Rectangle();
isRight = true;
isShotFired = false;
isBulletOut = false;
isBulletAlive = true;

Update Code:

 KeyboardState keys = Keyboard.GetState();
 if ((keys.IsKeyDown(Keys.Space) == true || oldState.IsKeyUp(Keys.Space) == false))
        {
            recBullet.X = recTank.X + recTank.Width / 2;
            recBullet.Y = screenHeight;
            isShotFired = true;
            isBulletAlive = true;
        }if (isShotFired)
        {
            recBullet.Y -= 10;
        }
        if (recBullet.Y <= 0)
        {
            isShotFired = false; 
        }
        for (int x = 0; x < numberOfXInvaders; x++)
        {
            for (int y = 0; y < numberofYInvaders; y++)
            {
                if (isBulletAlive)
                {
                    if (recBullet.Intersects(recInvader[x, y]))
                    {
                        if (!isInvaderDead[x, y])
                        {
                            isInvaderDead[x, y] = true;
                            isBulletAlive = false;
                        }

                    }
                }
            }
        }
oldState = keys;

Draw Code:

if (isBulletAlive)
        {
            spriteBatch.Draw(texBullet, recBullet, Color.Green);
        }

I appreciate any help I can get for this! Thanks.

Upvotes: 0

Views: 58

Answers (1)

Luke Villanueva
Luke Villanueva

Reputation: 2070

Do the following steps:

  1. First check if your bullet image is being loaded properly
  2. Try to draw it on top of all your images. Put it on the last part of your Draw method.
  3. If it is still not there, try to comment out all your spriteBatch.Draw and leave the draw for your bullet. If it's there, try to check your logic on your draw. Some images might be overlapping.
  4. If steps 2 and 3 fails, repeat step 1

Upvotes: 1

Related Questions