问题描述
只要好奇,如果我在另一个动画片段内嵌入一个动画片段,并且嵌套的动画片段被附加到一个自定义类别。那个自定义类有一个Event.ENTER_FRAME监听器。当我摧毁容器movieclip,将闪烁摆脱嵌套movieclip的自定义类内的事件监听器?可能看起来像一个愚蠢的问题,但闪光有一个倾向一旦将其从舞台上移除(这是通过addChild应用动画片段),将动画片与动画片本身一起杀死。当事情变得太复杂的时候它是一种ha ha。我的解决方案是为我的所有对象创建一个单一的循环。现在我来到一个点,我可能需要在另一个嵌套一些movieclip,所以嵌套的movieclip的位置保持相对。父级动画片段将旋转和缩放,所以我宁愿嵌套它。但是我知道flash是如何处理嵌入到父级动画片段中的moveiclips,并且已经输入了框架监听器。希望这不是太混乱。谢谢!
好吧,首先,当我的笔记本电脑电池完全死亡时,两个...
您的问题有点混乱,但我认为这部分是本质你要问什么,所以我会尝试根据我如何解释这个来回答它。
如果您有一个自定义显示对象,并且在该类内添加了用于输入框架或鼠标事件的侦听器,并将自定义类的实例添加到另一个显示列表显示对象时,将需要确保删除任何侦听器,并在删除父剪辑时,去引用对自定义类的任何外部引用。
你可以通过在自定义类中为 Event.REMOVED
添加一个监听器,或 Event.REMOVED_FROM_STAGE
事件。在这些事件的处理程序中,您可以删除任何侦听器并取消引用任何可能使垃圾回收器忽略任何其他符合条件的对象的对象。这种方法允许您使用removeChild(myCustomInstance)从阶段中删除自定义类的实例,并在一个整洁的方法调用中调用任何清理。
PsuedoCode:
pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
addEventListener(Event.REMOVED_FROM_STAGE,onRemovedHandler);
保护功能onRemovedHandler(event:Event)
{
removeEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
removeEventListener(Event.REMOVED_FROM_STAGE,onRemovedHandler);
$ b $你可能也想用
weakReference
在调用 addEventListener
时设置为true。我没有太多的经验w / weakReference
,因为我更喜欢手动删除引用。 以下是Adobe关于Flash Player GC的一篇简短文章: b
$ b
Just curious, If i nest a movieclip inside of another movieclip, and the nested movieclip is attached to a custom class. And that custom class has a Event.ENTER_FRAME listener. when I destroy the container movieclip, will flash get rid of the event listeners inside of nested movieclip's custom class ?
May seem like a silly question but flash has a tendency of not killing listeners along with the movieclip itself once it is removed from the stage(this is if your apply the movieclip via addChild). and its a hastle when things get too complex. My solution was to create a single loop for all my objects. Now ive come to a point where I am probably going to have to nest some movieclips inside of another so the position of the nested movieclip stays relative. the parent movieclip will be rotating and scaling so I rather nest it. But I got to know how flash handles moveiclips that are nested into a parent movieclip and has enter frame listeners. hope this isnt too confusing. thanks!
Ok, First of all I was nearly done w/ this answer when my laptop battery completely died so this is round two...
Your question is a bit confusing, though I think this part of it is the essence of what you are asking so I'll try to answer it based on how I interpret this.
If you have a custom display object and inside that class you add listeners for enter frame or mouse events and add an instance of the custom class to the display list of a another display object, you will need to make sure to remove any listeners and de-reference any external references to the custom class when the parent clip is removed.
One way you can go about this is by adding a listener in your custom class for either the Event.REMOVED
or Event.REMOVED_FROM_STAGE
events. Inside the handler for either of these events you can remove any listeners and de-reference any objects that might make the garbage collector ignore any otherwise eligible objects. This approach allows you to remove instances of your custom class from the stage using removeChild( myCustomInstance ) and invoking any cleanup in one neat method call.
PsuedoCode:
public class MyCustomDisplayObject
{
public function MyCustomDisplayObject()
{
addEventListener( Event.ENTER_FRAME, onEnterFrameHandler );
addEventListener( Event.REMOVED_FROM_STAGE, onRemovedHandler );
}
protected function onRemovedHandler( event:Event )
{
removeEventListener( Event.ENTER_FRAME, onEnterFrameHandler );
removeEventListener( Event.REMOVED_FROM_STAGE, onRemovedHandler );
}
}
You may also want to look into using weakReference
set to true in your calls to addEventListener
. I don't have much experience w/ weakReference
as I prefer to manually remove references.
Here is a short article from Adobe about Flash Player GC:
http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html
这篇关于当涉及到将movieclip直接添加到另一个时,flash如何处理垃圾回收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!