I am developing a game where objects move in the screen. My object is instantiated in Y = 0 and I want to destroy this object when it goes under the screen Height. So if (positionY >= ScreenHeight) I have to destroy this object to clean my game and prevent "lag".

How to do this? I have a dispose method (this.dispose) but it cause the dispose of the app and I want destroy only the object.

Thanks in advance


It's not a good idea to dispose/destroy your objects unless you want to change your screen (for example moving from menu to game level ) because when your are disposing/destroying objects , garbage collector is called ,so it's better to store your dead objects in memory instead of destroying them.

First off all read this article https://github.com/libgdx/libgdx/wiki/Memory-management specially Object pooling.

Pooling objects allow you to reusing your dead objects instead of recreating them.For example in your case you have several ways to reuse your dead object like this

  • \$\begingroup\$ I'll wait for others answer but maybe yours is the best \$\endgroup\$
    – user65825
    May 13 '15 at 20:16
  • \$\begingroup\$ Unless you are creating thousands of objects, say to simulate rain, a pool might be more complexity than you need, in which case a simple array of objects should suffice. \$\endgroup\$ May 13 '15 at 20:48
  • \$\begingroup\$ Yes of course there are different ways in different situations but important thing is avoid destroying objects \$\endgroup\$ May 14 '15 at 8:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .