Sign in to follow this  
Dark12345

Destroying Bullets / Enemies

Recommended Posts

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...
[code] if (powerCollision == true)
{
powerUpPos = new Vector2(-5000, -5000);



}
[/code]

Share this post


Link to post
Share on other sites
You can maintain a [b]bool isVisible[/b] 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 [b]if( isVisible ) powerUp->Render()[/b].

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this