Results 1 to 5 of 5
  1. #1
    Muqali is offline Member
    Join Date
    Oct 2014
    Posts
    2
    Rep Power
    0

    Default How can I delete objects?

    Hello all, I am a fairly new programmer (< 5 months) I am using libgdx and box2d to try to make a simple cannon game.

    When I have the cannonball collide with another object I want to delete that object and have it stop calling all of it's functions. It doesn't seem like I can have it delete itself, and the contactListener has a reference to the cannonball and other object, but if I set either to null, it seems the original reference still exists and the object isn't deleted. This seems like it could be a common issue, how is this solved?

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How can I delete objects?

    You should research into Java garbage collection.

    An object is cleaned up if there are no hard references to it anymore anywhere, at a time where the runtime deems it necessary. So if it isn't cleaned up, then probably it is still referenced somewhere else in the code. But it may just be that there is still plenty of memory available and the garbage collection algorithm that is in effect simply doesn't do anything yet.

    At this point you have no proof that there is something to actually solve. A real problem would be an OutOfMemory error which proves that objects are being retained which cannot be cleaned up by the runtime.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Muqali is offline Member
    Join Date
    Oct 2014
    Posts
    2
    Rep Power
    0

    Default Re: How can I delete objects?

    I have an "Enemy" object that has a child "Body" object. It also has update functions that work relative that body. The only thing I can think of is to have a dispose function to destroy the body and then add if(body != null) to the update functions. However, doesn't that mean that that every update I will lose some processing power on all of these ifs? Especially if these "Enemys" are being constantly generated.

    The problem I think I'm having is that I have a contactListener class that has functions I don't explicitly call in my program.

    ContactListener (libgdx API).

    If I can't directly pass the only reference to my "Enemy" object into the contactListener functions, then I don't appear to have a way to delete them on contact.

    Hopefully this makes some sense.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How can I delete objects?

    ah, so you are not really talking about Java object deletion but rather about that enemy disappearing from your game. Am I correct?

    In other words your true question would be a LibGDX API specific one.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How can I delete objects?

    I keep reading about similar situations. When an object is created it is kept somewhere. Perhaps a list or an array. That list is probably used to keep track of what to render on the screen. I would think there has to be some method for removing an object from a list of "active" objects.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. how to specify how many objects you want to delete
    By Snake006 in forum New To Java
    Replies: 14
    Last Post: 10-15-2014, 05:27 PM
  2. Replies: 3
    Last Post: 07-31-2014, 10:47 AM
  3. Sample to create, insert, modify and delete MySQL Objects
    By dbblogger in forum Reviews / Advertising
    Replies: 3
    Last Post: 06-30-2012, 12:24 PM
  4. Replies: 2
    Last Post: 04-20-2009, 09:00 AM
  5. Replies: 1
    Last Post: 01-22-2009, 05:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •