Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
134
rated 0 times [  137] [ 3]  / answers: 1 / hits: 17861  / 10 Years ago, sat, july 5, 2014, 12:00:00

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.


More From » html

 Answers
6

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


[#70304] Thursday, July 3, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jelani

Total Points: 473
Total Questions: 99
Total Answers: 99

Location: Cape Verde
Member since Fri, Nov 27, 2020
4 Years ago
;