Light_Warrior
Light_Warrior

Reputation: 183

How to make bullet holes appear in the background? (XNA C#)

I am trying to make bullet holes to appear in the background once I fire the gun.

So far the game is working like this: 1. Load gun sprite 2. Click left mouse button load gunFlame sprite and play sounds 3. Release left mouse button load gun sprite (without the flame) The next step I want to implement is to have some bullet holes on the background at a relative distance from the gun.

class Gun
    {
        Texture2D gun, gunFlame, gunObject, bulletHole, hole;
        Vector2 position, bulletHolePosition;
        SoundEffect shotSound, fallSound;
        MouseState currentMouseState, previousMouseState;

        public Gun(ContentManager Content)
        {
            bulletHole = Content.Load<Texture2D>("bullethole");
            bulletHolePosition = new Vector2(position.X + 3, position.Y + 3);
            gun = Content.Load<Texture2D>("Gun");
            position = new Vector2(10, 10);
            gunFlame = Content.Load<Texture2D>("gunFlame");
            shotSound = Content.Load<SoundEffect>("gunshot");
            fallSound = Content.Load<SoundEffect>("bullet-fall");
            gunObject = gun;

        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(gunObject, position, Color.White);

        }


        public void Update(GameTime gameTime)
        {
            // Keep track of the previous state to only play the effect on new clicks and not when the button is held down
            previousMouseState = currentMouseState;
            currentMouseState = Mouse.GetState();
            if (currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton != ButtonState.Pressed)
            {
                gunObject = gunFlame;
                hole = bulletHole;
                shotSound.Play();
                fallSound.Play();           
            }
            else if(currentMouseState.LeftButton == ButtonState.Released)
            {
                 gunObject = gun;
            }  
        }

Upvotes: 1

Views: 112

Answers (1)

Nahuel Ianni
Nahuel Ianni

Reputation: 3185

I see some problems in your code, which makes me doubt you can even execute it, for example in your constructor you have:

bulletHolePosition = new Vector2(position.X + 3, position.Y + 3);
gun = Content.Load<Texture2D>("Gun");
position = new Vector2(10, 10);

You are using the position object before initializing it, is that intended?

Anyway, what you could do in order to create bullet holes is to have a list (or array or whatever you like best) containing the positions where the holes should be in your background.

You add holes to the collection inside the if (currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton != ButtonState.Pressed) statement.

After that, on the Draw method, just iterate through the collection and draw the bulletHole texture on each position.

So basically replace Vector2 bulletHolePosition with IList<Vector2> bulletHolePositions.

Example:

Vector2 position;
IList<Vector2> bulletHolePositions;

In the constructor:

bulletHolePositions = new List<Vector2>;

In the Update method:

if (currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton != ButtonState.Pressed)
{
    bulletHolePositions.Add(new Vector2(currentMouseState.X, currentMouseState.Y));
    //...
}
//...

And finally, in the Draw method:

spriteBatch.Draw(gunObject, position, Color.White);
foreach(var holePosition in bulletHolePositions)
    spriteBatch.Draw(bulletHole , position, Color.White);

Keep in mind that if you have MANY of these on the screen, your game performance may drop.

If you need more help, please give us more details about where you are having problems instead.

Upvotes: 1

Related Questions