在Phaser中摧毁精灵

ale*_*nia 17 javascript html5 sprite phaser-framework

我在Phaser中摧毁精灵时遇到了麻烦.

我有一个JavaScript对象,我们称之为Block.Block有一个sprite属性,设置如下:

this.sprite = this.game.add.sprite(this.x, this.y, 'blocks', this.color);
Run Code Online (Sandbox Code Playgroud)

在我的代码中的某个点,Block由两个不同的数组引用:

square[0] = Block;
destroy[0] = Block;
Run Code Online (Sandbox Code Playgroud)

在某个Update()循环中,我需要销毁精灵,所以我使用以下代码:

square[0].sprite.destroy(true); //Destroy the sprite.
square[0] = null; //Remove the reference.
Run Code Online (Sandbox Code Playgroud)

在下一个Update()循环中,当我查看destroy [0]时,我希望看到:

destroy[0].sprite: null
Run Code Online (Sandbox Code Playgroud)

然而,我所看到的是:

destroy[0].sprite: b.Sprite
Run Code Online (Sandbox Code Playgroud)

属性只是默认并设置为false.我担心的是,如果我现在将destroy [0]设置为null,那个sprite对象会发生什么?

它会浮动还是自动清理?我应该先以某种方式破坏Block对象吗?另外,如果destroy()没有使引用为空,那么它与kill()有什么不同?

任何有关此事的想法将不胜感激.

小智 22

杀戮与毁灭之间的区别

Kill应该暂停渲染,但对象仍然存在.如果你想制作一个可重用的对象,这是很好的.您可以再次创建对象,而无需再次实际创建对象.

Destroy应删除对象及其相关的所有内容.当您想要将对象发送到垃圾收集器时,可以使用此方法.

请注意,对于某些文本对象,您无法使用kill,您只能使用destroy

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