Destroying Bullets / Enemies

Started by
2 comments, last by Dark12345 13 years, 2 months ago
Hi ,
Me again , using XNA 4.0 (C#).
Just wondering how id go about actually destroying bullets or enemies?.

Right now , my bullet , and enemy are 2D textures.
At the moment im kind of cheating , when the player let goes of space , the texture moves to like -5000,-5000 (off the screen)

This is obviously a bad way to do it , and if the player holds the fire button , the texture seems to stay in the last position.

If i can get a way to destroy the bullet , the enemies would be just as easy.

heres an example of what im doing for my powerup...
if (powerCollision == true)
{
powerUpPos = new Vector2(-5000, -5000);



}

Advertisement
You can maintain a bool isVisible variable, enabling it and disabling it. If disabled, don't render it. If you're rendering offscreen items and just letting them be culled because they're offscreen, you're losing performance. It would be quicker to do if( isVisible ) powerUp->Render().

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

In managed languages (such as C# and Java), once there are no more references to an object, it will automatically be deleted. Just remove any references to your bullet. If it is in a list, remove it from the list. If it is just a single item, set the variable to null. This does mean you will probably need to put your removal logic outside of the object itself.
Cheers Buckeye , your idea worked perfect.
Now when i hold space my ship fires , and the image becomes unvisible right after. and repeats as long as space is held!

This topic is closed to new replies.

Advertisement