• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
scole127

Help deleting an object in visual C# XNA

9 posts in this topic

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);
        }
 
 
 
    }
}]
Edited by scole127
0

Share this post


Link to post
Share on other sites

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 ])

Edited by Andy474
0

Share this post


Link to post
Share on other sites

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

Edited by Andy474
0

Share this post


Link to post
Share on other sites

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"

0

Share this post


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

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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.

0

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  
Followers 0