[SOLVED] Unable to destroy nodes by itself

Hi,

I’m trying to destroy a node by itself. Is it possible? I’ve created a clone from a bullet node I put in the scene, in a component script called Aircraft that is attached to Aircraft node, like this (in update() function as it is called every N tick):

...
var scene = cc.director.getScene();
		
		
var bullet = cc.instantiate(this.bullet);

bullet.parent = scene;
bullet.setPosition(this.node.x, this.node.y);
...

Then as it moves to the top and collides to a top wall node, it should get destroyed. I do that on the bullet’s own component script called Bullet, like this:

onCollisionEnter: function(other, self) {
	if (other.node.group === "wall") {
		this.node.destroy();
	}
}

If I do this, visually, the bullets disappear from the screen. But my frame time keeps increasing significantly by game logic, and just a little from the renderer. I assume there’s something going on with the bullet node not being destroyed properly, which is why I’m here.

If I immediately delete after instantiation, in Aircraft script, there is no problem.

Anyone can help?

Cheers,

Alectora

I think your script is correct. Maybe the numbers of destroyed bullets is too small comparing to number of new bullets? Or you can try to use Node Pool to reuse bullet, instead of cloning them.

Thanks for the reply. I’ve counted the total number of deleted bullets vs created bullets, and I can be sure it is deleted immediately once it reaches the wall. I was just testing how the game look so no optimization yet. I’ll try the Node Pool then. I just fear this can become a bigger problem as I add (and delete) more objects that don’t need a pool.

BugsKiller, thanks for bringing up the clone issue. It was my all my mistake. :joy:

I put all objects (bullet, and aircraft) to replicate outside the canvas, and apparently I cloned the aircraft too, and the original aircraft apparently released the bullets OUTSIDE the wall, hence the bullets from the original aircraft don’t get destroyed. I assume my count was wrong as well.

Thanks for the help guys it’s solved now. :persevere:

Do not create and destroy nodes, it has a great impact in performance. Memory will be cleaned up by garbage collector (eventually).

Use object pool: http://www.cocos2d-x.org/docs/api-ref/creator/v1.5/classes/pool.html
It will save you a lot of performance issues and it is a more elegant way to reuse memory.

Yes I’ll use that when the game is on optimization phase, cause there’s a lot of bullets going on. I was testing how things look using Cocos Creator. Thanks for the reminder! :slight_smile:

1 Like