How can I delete a sprite in Monogame, after clicking on it?

1.6k Views Asked by At

I am writing a c# monogame. I have a hero and he can shoot. I want that, when he hits a sprite(here: a simple ball), than this ball should be deleted on the map.

This is my drawing method

public void Draw(GameTime gameTime)
{
            mMap.Draw(ScreenManager.mSprites);
            ScreenManager.mSprites.Begin();
            ScreenManager.mSprites.Draw(mTest, mHeroPos, Color.White);
            ScreenManager.mSprites.Draw(mBall, new Rectangle(120, 120, 50, 50), Color.White);
            DrawEffects();
            ScreenManager.mSprites.End();
}

I don't have code for deleting the ball on the map, because I don't have an idea for it. I have more code, but it is to much to show it here and I think it is not important for my problem. In this code, my hero can walk and shoot on sprites. Everything works perfect. All in all my problem ist, that I want to shoot on an sprite and after that the sprite should be disapered. Thanks for help

1

There are 1 best solutions below

2
abousquet On

You code is limited, but there is multiple way you could do this.

Your best bet, in my opinion, would be to create a new class to represent the ball and make it so that it has a "IsAlive" property. Once the ball is dead, you mark it as "IsAlive = false". In you draw method, you check if the ball is alive before drawing it.

Another way, since you will probably eventually have multiple balls, is to have an array of those ball objects. Once an object is killed, your remove it from the array. In your draw method, you only render objects that are in the array.