Differenze between Add and Recycle?


(fedyfausto) #1

Hello to all :slight_smile: i have a lot of Enemys in my levels and for anyone i use Add(new enemy) but how can i use the Recycle method for conserve more memory ? :frowning:


(Alex Larioza) #2

It should be noted that object pooling is only going to improve performance if you’re calling the “new” operator constantly, for example, if you’re constantly destroying and creating enemies.

The way I go about object pooling is giving the entities an init() function that is called when the object is created. Here’s an example class:

public function MyEntity()
{
super();
}

public function init(x:Number, y:Number):Entity
{
this.x = x;
this.y = y;
return this;
}

The following then calls the init() function and passes the relevant parameters to the entity before it is added to the world:

MyEntity(world.create(MyEntity)).init(10,120);

Memory slowly ticks up despite removing entities from the world [Solved]