AS3:严重放缓

发布于 2024-11-29 03:35:59 字数 133 浏览 0 评论 0原文

我正在开发一款 Flash 游戏,运行游戏一段时间后,帧速率大幅下降。屏幕上同时显示的影片剪辑并不多,但经常使用removeChild 和addChild 来替换影片剪辑。

如何测试内存泄漏等问题?在这方面有哪些好的 AS3 编程标准?

I'm working on a Flash game, and after running my game for a while there is a huge drop in frame rate. There aren't a lot of MovieClips onscreen at once, but MovieClips are being replaced using removeChild and addChild often.

How can one test for problems such as memory leaks? And what are some good AS3 programming standards on this matter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深者入戏 2024-12-06 03:35:59

您似乎没有为垃圾回收准备 MovieClip 实例。 此线程可以对你非常有帮助。

正确丢弃 MovieClip(或任何其他对象)时需要涵盖的一些基本内容是:

  1. DisplayList 中删除对象(如果它是 DisplayObject)。这是通过您已经执行的操作来完成的,removeChild()
  2. 删除已应用于该对象的所有事件侦听器。最好的办法是从一开始就掌握这一点;我的意思是,当您调用 addEventListener() 时,请务必在不久的将来的某个地方添加一个姊妹 removeEventListener()
  3. 删除对您的对象的引用。这包括但不限于:通过成为数组/向量的一部分来引用对象、通过存储在另一个对象的属性中来引用等。

我可以提供的建议是在您的基类中包含对象是处理所有这些的方法,例如remove()deconstruct()

这是一个示例:

public function deconstruct():void
{
    if(parent)
        parent.removeChild(this);

    removeEventListener(MouseEvent.CLICK, _onClick);
}

当您扩展此类并需要其他取消引用功能时,只需构建您的 deconstruct() 方法:

override public function deconstruct():void
{
    removeEventListener(MouseEvent.MOUSE_OVER, _mouseOver);

    var i:int = someArray.indexOf(this);
    someArray.splice(i, 1);

    super.deconstruct();
}

It seems like you're not preparing your instances of MovieClip for garbage collection. This thread could be extremely helpful to you.

Some of the basic things you want to cover when discarding a MovieClip (or any other Object) properly are:

  1. Remove the object from the DisplayList (if it's a DisplayObject). This is done via what you're doing already, removeChild()
  2. Remove any event listeners that have been applied to the Object. Best thing to do is keep on top of this right from the beginning; by that I mean, when you call addEventListener(), be sure to somewhere in the very near future add a sister removeEventListener() as well.
  3. Remove reference to your Object. This includes, but is not limited to: reference to the Object via being part of an Array/Vector, reference via being stored in a property of another Object, etc.

A suggestion that I can offer is to have in the base class of your objects a method that handles all of this, eg remove() or deconstruct().

Here's an example:

public function deconstruct():void
{
    if(parent)
        parent.removeChild(this);

    removeEventListener(MouseEvent.CLICK, _onClick);
}

And when you extend this class and need other dereferencing features, just build on your deconstruct() method:

override public function deconstruct():void
{
    removeEventListener(MouseEvent.MOUSE_OVER, _mouseOver);

    var i:int = someArray.indexOf(this);
    someArray.splice(i, 1);

    super.deconstruct();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文