javascript 在 Phaser 中销毁精灵
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24590371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Destroying sprites in Phaser
提问by alexania
I'm having trouble destroying Sprites in Phaser.
我在 Phaser 中破坏 Sprite 时遇到问题。
I have a JavaScript object, let's call it Block. Block has a sprite property, that gets set like so:
我有一个 JavaScript 对象,我们称之为 Block。Block 有一个 sprite 属性,设置如下:
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:
在我的代码中的某个点,Block 被两个不同的数组引用:
square[0] = Block;
destroy[0] = Block;
On a certain Update() cycle, I need to destroy the sprite, so I'm using the following code:
在某个 Update() 循环中,我需要销毁精灵,因此我使用以下代码:
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:
在下一个 Update() 循环中,当我查看 destroy[0] 时,我希望看到:
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?
属性刚刚默认并设置为false。我担心的是,如果我现在将 destroy[0] 设置为 null,那个 sprite 对象会发生什么?
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()?
它会四处漂浮还是会自动清理?我应该先以某种方式销毁 Block 对象吗?另外,如果 destroy() 没有将引用清零,它与 kill() 有什么不同?
Any thoughts on the matter will be greatly appreciated.
对此事的任何想法将不胜感激。
回答by Ibnu Triyono
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.
Kill
应该停止渲染,但对象仍然存在。如果你想制作一个可重用的对象,这很好。您可以再次创建对象,而无需再次实际创建对象。
Destroy
should remove the object and everything related to it. You use this when you want to send the object to the garbage collector.
Destroy
应该删除该对象以及与之相关的所有内容。当您想将对象发送到垃圾收集器时,您可以使用它。
Please note that for some objects like text, you can't use kill
, you can only use destroy
请注意,对于文本等某些对象,您不能使用kill
,您只能使用destroy
Reference: http://www.html5gamedevs.com/topic/1721-how-to-remove-text/#entry12347
参考:http: //www.html5gamedevs.com/topic/1721-how-to-remove-text/#entry12347
回答by James L.
@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.
@ibnu 是正确的。Destroy
核对对象,同时kill
停止渲染。但是,您的问题与内存泄漏和 GC 相关。我不是 GC 专家,但这就是我认为正在发生的事情。
//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.
但destroy[0].sprite
仍然保留对您“被摧毁”的精灵的引用。this.sprite
可能也是。这是因为 Phaser destroy 方法仅从对象中删除 Phaser 特定属性。JS 负责通用对象垃圾回收。该对象正在逃避,因为您在范围内仍然有有效的引用。
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 leakvariables across difference scopes.
通过从作用域中删除引用destroy[0].sprite = null
或等待下一个状态更改作用域来解决此问题(假设 destroy 不是静态变量)。你不必自己管理内存资源,JS != C。只要确保你没有跨不同作用域泄漏变量。
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)
什么是 JavaScript 垃圾回收?(虽然我认为该delete
命令不再推荐用于 GC,但在 Phaser 中肯定没有必要)