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.


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


@ibnu is correct. Destroy nukes the object, while kill halts rendering. However, your question is relating to memory leaks and GC. I'm no GC pro, but here's what I think is happening.

//create an object
this.sprite = this.game.add.sprite(this.x, this.y, 'blocks', this.color);
//create additional references to the object
square[0] = Block;
destroy[0] = Block;
//destroy the object via Phaser's call. Remove 1/2 reference
square[0].sprite.destroy(true); //Destroy the sprite.
square[0] = null; //Remove the reference.

But destroy[0].sprite still holds a reference to your "destroyed" sprite. this.sprite probably does too. That's because the Phaser destroy method only removes Phaser specific properties from the object. JS is in charge of generic object Garbage Collection. The object is escaping that because you still have valid references in scope.

Fix this problem by removing the reference from scope destroy[0].sprite = null or by waiting for the next state to change the scope (assuming destroy is not a static var). You do not have to manage memory resources youself, JS != C. Just make sure you don't leak variables across difference scopes.

What is JavaScript garbage collection? (though I don't think the delete command is recommended for GC anymore, it's certainly not necessary in Phaser)

链接地址: http://www.djcxy.com/p/27178.html

上一篇: Javascript内存使用管理

下一篇: 在Phaser中摧毁精灵