We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › How to delete an instance of an object
Page Index Toggle Pages: 1
How to delete an instance of an object? (Read 2868 times)
How to delete an instance of an object?
Jul 28th, 2009, 8:46pm
 
I'm having trouble figuring out how to remove an object. My basic "game" is for a square to fall into a basket or to hit the ground. If either of these events happen then I want the square to disappear. How would I do that?
Re: How to delete an instance of an object?
Reply #1 - Jul 29th, 2009, 12:20am
 
More often than not you're likely to re-use objects, especially in games, so a common approach is simply to either shift them off screen so they can move back onscreen later, or to use a boolean to determine whether they should be drawn or not.  So instead of creating a new object for each ball, or in your case square, you simply re-use the same one over and over again.

I must admit I'm not sure how you really delete a class object - though no doubt it's going to be one of those head-slapping moments when someone provides an answer.  Though having said that if you're using multiple objects then you'd do well to store these in an array, in which case you could use one of the array methods to remove it.
Re: How to delete an instance of an object?
Reply #2 - Jul 29th, 2009, 12:33am
 
blindfish wrote on Jul 29th, 2009, 12:20am:
I'm not sure how you really delete a class object
That's simple: you just no longer reference it!
For example: Foo f = new Foo("A");, you created an object of class Foo. Now if you do: f = null; or f = new Foo("B"); you no longer reference the first object (unless you have assigned another variable with f, of course) so the garbage collector will take care of it.

jamboree, as blindfish said, in Processing, to remove an object from the sketch, you just no longer draw it: the scene is permanently redraw multiple times per second, at least if you use a background() call at the start of draw().
Re: How to delete an instance of an object?
Reply #3 - Jul 29th, 2009, 9:20am
 
Thanks for both of the answers. So what I'm getting at is that if processing is no longer drawing the object is it in effect deleted Or I guess what I'm wondering is if I move the object of the screen or do not display it is it still taking up memory

Also I had another question about this:
blindfish wrote on Jul 29th, 2009, 12:20am:
So instead of creating a new object for each ball, or in your case square, you simply re-use the same one over and over again.


I'm thinking about having multiple fruits falling to the ground at a time so at times there will be fruits falling and fruits disappearing. Would it make more sense to have the fruits as their own objects along with I guess a controller class for the whole system or one object being the controller system. And if I make just one object that controls the entire thing how would multiple instances of the fruit work
Re: How to delete an instance of an object?
Reply #4 - Jul 29th, 2009, 9:52am
 
"if processing is no longer drawing the object is it in effect deleted"
Deleted from screen/display, not from memory. See my previous answer how to delete from memory (you don't, but you manage so the GC does it).

Not too sure about your controller question, but "how would multiple instances of the fruit work" might be answered with: by putting them in an array, for example.
Re: How to delete an instance of an object?
Reply #5 - Jul 30th, 2009, 2:21am
 
To clarify - you'd only move an object off screen if you knew you were going to use it again very soon:  this saves you having to delete the object and create a new one in its place - which I'd guess is more efficient.  A good example of this in practice would be a game in which a bunch of aliens moves down the screen.  When an alien reaches the bottom, rather than deleting it and creating a new one, you just reposition it above the top of the screen.

In the case of your fruit there are several possible approaches.  In terms of the fruit objects themselves I'd either have a single fruit class and use a parameter to determine which fruit is drawn or take the slightly more complicated approach of having a fruit superclass - which would handle all the fruit generic variables and methods - and inherit from this for the individual fruit, though I suspect that would be overkill.

I personally wouldn't bother creating a 'controller object' - I'd just create appropriate functions and handle that stuff in the draw loop, but then that's just me being a bit lazy...

As for arrays of objects - that suggestion seems to get made a lot.  I'm pretty sure PhilHo or someone gave a good example of this in practice in a recent thread.  I also just found this example which you might find useful.
Page Index Toggle Pages: 1