Help deleting an object in visual C# XNA

Started by
8 comments, last by NightCreature83 10 years, 11 months ago

Hi, new to this site

I'm a beginner at programming and have a school project that involves remaking the Atari parachute game in Visual C# 2010 Express.

My question is when i have released the parachute how do i delete the object when it collides with the birds.

Any help would be great. As mentioned before I'm new to this so please give easy to follow steps.
Thanks.


(There is some code referring to a class called Plane but i ended up finding a different way to do the plane but i didn't want to delete it yet in case i need).


Game 1 class:


[using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
// Plane PlaneObject;
Truck TruckObject;
//Parachute ParachuteObject;
Bullet BulletObject;

Texture2D planeTexture;
Vector2 planePosition;

bool personHit = false;




Birds[] ballObjects;
int Birdcount = 3;



//Random for the Birds
Random rand = new Random();


public static bool canPress = true;





public Vector2 SpriteLocation;
public Game1()

{

graphics = new GraphicsDeviceManager(this);

////////// Window size //////////////////
graphics.PreferredBackBufferHeight = 1080;
graphics.PreferredBackBufferWidth = 1920;
graphics.IsFullScreen = true;
////////// Window size //////////////////


Content.RootDirectory = "Content";
}

protected override void Initialize()
{


planePosition = new Vector2 (10,10);







ballObjects = new Birds[Birdcount];


for (int index = 0; index < Birdcount; index++)
{

////Color For The Random Collor////
byte r = (byte)rand.Next(64, 256);
byte g = (byte)rand.Next(64, 256);
byte b = (byte)rand.Next(64, 256);
byte a = (byte)rand.Next(64, 256);
Color tempColor = new Color(r, g, b, a);

ballObjects[index] = new Birds(Birds.Texture, new Vector2(rand.Next(10, 600), (rand.Next(10, 600))), new Vector2(rand.Next(-10, 10), (rand.Next(-10, 10))), tempColor);

}





// PlaneObject = new Plane(Plane.Texture, new Vector2(1200f, 10f), new Vector2(-10f, 0f), Color.White);
TruckObject = new Truck(Truck.Texture, new Vector2(2000, 800f), new Vector2(-3f, 0f), Color.White);
// ParachuteObject = new Parachute(Parachute.Texture, new Vector2(400f, 0f), new Vector2(0f, 3f), Color.White);

base.Initialize();


}


protected override void LoadContent()
{

spriteBatch = new SpriteBatch(GraphicsDevice);

// Plane.Texture = Content.Load<Texture2D>("Sprites\\Plane");
// Plane.GraphicsViewport = graphics.GraphicsDevice.Viewport;

Truck.Texture = Content.Load<Texture2D>("Sprites\\Truck");
Truck.GraphicsViewport = graphics.GraphicsDevice.Viewport;

// Parachute.Texture = Content.Load<Texture2D>("Sprites\\Parachute");
// Parachute.GraphicsViewport = graphics.GraphicsDevice.Viewport;

Birds.Texture = Content.Load<Texture2D>("Sprites\\Bird");
Birds.GraphicsViewport = graphics.GraphicsDevice.Viewport;

Bullet.Texture = Content.Load<Texture2D>("Sprites\\Parachute");
Bullet.GraphicsViewport = graphics.GraphicsDevice.Viewport;

planeTexture = Content.Load<Texture2D>("Sprites\\Plane");


}

protected override void UnloadContent()
{

}

protected override void Update(GameTime gameTime)
{

personHit = false;


if (planePosition.X > 2000f)
{
planePosition.X = -600f;
}







planePosition.X = planePosition.X + 7f;


//////////////////////////////Movement Control For Parachute///////////////////////////////////////

if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();


if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
BulletObject.Position.X = BulletObject.Position.X + 3f;
}


if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
BulletObject.Position.X = BulletObject.Position.X - 3f;
}

//////////////////////////////Movement Control For Parachute///////////////////////////////////////


foreach (Birds ball in ballObjects)
{
ball.Update();


Rectangle ballRectangle = new Rectangle((int)ball.Position.X, (int)ball.Position.Y, Birds.Texture.Width, Birds.Texture.Height);


Rectangle planeRectangle = new Rectangle((int)planePosition.X, (int)planePosition.Y, planeTexture.Width, planeTexture.Height);

if (planeRectangle.Intersects(ballRectangle))
{
personHit = true;

}

}
// PlaneObject.Update();
TruckObject.Update();
// ParachuteObject.Update()


if (canPress)
{

if (Keyboard.GetState().IsKeyDown(Keys.Space))
{

Vector2 bulletPosition = planePosition;
BulletObject = new Bullet(Bullet.Texture, bulletPosition);
Bullet.alive = true;

}


}


if (Bullet.alive)
{
canPress = false;
BulletObject.MovingBullet();
}



;

base.Update(gameTime);
}



protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.SkyBlue);

spriteBatch.Begin();

spriteBatch.Draw(planeTexture, planePosition, Color.White);
if (Bullet.alive)
{
BulletObject.Draw(spriteBatch);
}



// PlaneObject.Draw(spriteBatch);
TruckObject.Draw(spriteBatch);
// ParachuteObject.Draw(spriteBatch);


foreach (Birds ball in ballObjects)
{
ball.Draw(spriteBatch);
}






spriteBatch.End();


base.Draw(gameTime);
}
}
}]


Bullet class(Parachute, its named bullet because i was following a tutorial):


[using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
{
class Bullet
{
public Vector2 Position;
public static Texture2D Texture;
public static Viewport GraphicsViewport;
public static bool alive;


public Bullet(Texture2D newTexture, Vector2 newPosition)
{
Position = newPosition;
}





public void MovingBullet()
{
Position.Y = Position.Y + 2;

if (Position.Y > GraphicsViewport.Width - Texture.Width)
{
alive = false;
Game1.canPress = true;
}
}


public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw (Texture, Position, Color.White);
}






}

}]

Birds Class:

[using System;

using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace WindowsGame1
{
class Birds
{
public Vector2 Position;
public Vector2 Velocity;
Color Color;
public static Texture2D Texture;
public static Viewport GraphicsViewport;
public Birds(Texture2D newTexture, Vector2 newPosition, Vector2 newVelocity, Color newColor)
{
Position = newPosition;
Velocity = newVelocity;
Color = newColor;
}
public void Update()
{
Position = Position + Velocity;
if (Position.X < 0 || Position.X > GraphicsViewport.Width - Texture.Width)
{
Velocity.X = -Velocity.X;
Position.X = Position.X + Velocity.X;
}
if (Position.Y < 0 || Position.Y > GraphicsViewport.Height - Texture.Height)
{
Velocity.Y = -Velocity.Y;
Position.Y = Position.Y + Velocity.Y;
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, Position, Color);
}
}
}]

truck class:

[using System;

using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace WindowsGame1
{
class Truck
{
public Vector2 Position;
public Vector2 Velocity;
Color Color;
public static Texture2D Texture;
public static Viewport GraphicsViewport;
public Truck(Texture2D newTexture, Vector2 newPosition, Vector2 newVelocity, Color newColor)
{
Position = newPosition;
Velocity = newVelocity;
Color = newColor;
}
public void Update()
{
Position = Position + Velocity;
if (Position.X < -600f)
{
Position.X = 2000f;
}
}
// if (Position.Y < 0 || Position.Y > GraphicsViewport.Height - Texture.Height)
// {
// Velocity.Y = -Velocity.Y;
// Position.Y = Position.Y + Velocity.Y;
// }
//}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, Position, Color);
}
}
}]
Advertisement

By dividing by Zero.

Seriously, we're gunna need more info than that.

- What objects do you have?

- How do they work?

- Post your attempts already, we wont do your homework for you. (Hint: use code tags [ code ])

Ok, well You want to know when a collision has occured, So im not goign todo your homework for you but i'll give you this section of code


Rectangle PlayerRectangle = new Rectangle(0,0,100,100);
List<Rectangle> BirdRectangles = new List<Rectangle>();
 
foreach(Rectangle r in BirdRectangles)
{
    if(r.Intersects(PlayerRectangle))
     { //There is a collision! }

}

You are on the right lines smile.png looks good stuff so far (Please use [ code ] tags to paste code, it was hard to read.) Look into the code i put above and think about how to integrate it smile.png

Ok, well You want to know when a collision has occured, So im not goign todo your homework for you but i'll give you this section of code


Rectangle PlayerRectangle = new Rectangle(0,0,100,100);
List<Rectangle> BirdRectangles = new List<Rectangle>();
 
foreach(Rectangle r in BirdRectangles)
{
    if(r.Intersects(PlayerRectangle))
     { //There is a collision! }
}
 

Thank you for helping but the problem I'm having isn't looking for a collision i have that already I just need to know how to delete the parachute or "Bullet"

Also please use the [ code ] [ \code ] tags without the spaces between the "[", "]" and the inner text. It will make your code posts easier to read here.

In C# you don't delete objects garbage collection takes care of that for you, instead you let all the references you have to the object either scope out or set them to null. If it is a drawable object stop adding it to the drawing queue or stop calling it's draw method as well.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Thank you for helping but the problem I'm having isn't looking for a collision i have that already I just need to know how to delete the parachute or "Bullet"

Ok we as NightCreature said, you dont delete objects in C# if you want to remove it just set the object to null (this may cause NullReferenceExceptions) and then the garbage Collector will destroy it for you.

Or, if its in a List of some sort just .Remove(...) it.

Thank you for helping but the problem I'm having isn't looking for a collision i have that already I just need to know how to delete the parachute or "Bullet"

Ok we as NightCreature said, you dont delete objects in C# if you want to remove it just set the object to null (this may cause NullReferenceExceptions) and then the garbage Collector will destroy it for you.

Or, if its in a List of some sort just .Remove(...) it.

Thanks you are really helping but i cant figure out how to set an object to Null


String str = new String();
str = "Hello World!";
str = null;

Just assign it the value "null".

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Just a question related to the topic: doesn't Garbage Collector automatically dispose objects which are no longer required (in the same scope) regardless of ther value?

Just a question related to the topic: doesn't Garbage Collector automatically dispose objects which are no longer required (in the same scope) regardless of ther value?

Yes it does, but only the ones in the local space, if they are actually managed by a container you have to call a remove function on the container for that particular object. The null assignment is more for longer lifetime objects that aren't in a container, not that that situation arises a lot to be honest.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

This topic is closed to new replies.

Advertisement