Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destroying sprites in Phaser

I'm having trouble destroying Sprites in Phaser.

I have a JavaScript object, let's call it Block. Block has a sprite property, that gets set like so:

this.sprite = this.game.add.sprite(this.x, this.y, 'blocks', this.color);

At a certain point in my code, Block is referenced by two different arrays:

square[0] = Block;
destroy[0] = Block;

On a certain Update() cycle, I need to destroy the sprite, so I'm using the following code:

square[0].sprite.destroy(true); //Destroy the sprite.
square[0] = null; //Remove the reference.

On the next Update() cycle, when I look at destroy[0], I would expect to see:

destroy[0].sprite: null

However what I'm seeing is:

destroy[0].sprite: b.Sprite

With the properties just defaulted and set to false. My worry is, if I were to now set destroy[0] to null, what will happen to that sprite object?

Will it just float around or will it get cleaned up automatically? Should I be destroying the Block object first in some way? Also, if destroy() is not nulling the reference, how is it different from kill()?

Any thoughts on the matter will be greatly appreciated.

like image 969
alexania Avatar asked Jul 05 '14 20:07

alexania


1 Answers

Difference between Kill and Destroy

Kill is supposed to halt rendering, but the object still exists. It is good if you want to make a reusable object. You could create the object again without the cost of actually creating the object again.

Destroy should remove the object and everything related to it. You use this when you want to send the object to the garbage collector.

Please note that for some objects like text, you can't use kill, you can only use destroy

Reference: http://www.html5gamedevs.com/topic/1721-how-to-remove-text/#entry12347

like image 55
Ibnu Triyono Avatar answered Sep 19 '22 16:09

Ibnu Triyono