Actionscript 3 - 完全删除子项
我有一个对象数组,当另一个对象击中其中一个对象时,该对象将被删除。 我已使用removeChild()将其从舞台上删除,并使用splice()从数组中删除,但不知何故该对象仍在调用其一些函数,这会导致错误。 如何彻底摆脱一个物体? 也没有与其绑定的事件侦听器。
I have an array of objects that when another object hits one of them, the object will be removed. I have removed it from the stage using removeChild() and removed from the array using splice(), but somehow the object is still calling some of its functions which is causing errors. How do I completely get rid of an object? There are no event listeners tied to it either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
听起来您可能遇到了 Flash 播放器的垃圾收集问题。
Flash Player 10 中添加了一个新的 API,应该可以解决此问题:
unloadAndStop()
Grant Skinner 在他的博客上提供了有关此问题的更多信息:
http://www.gskinner.com/blog/archives/2008/ 07/unloadandstop_i.html
您可以通过以下网址获取 Flash Player 10 测试版:
http:// /labs.adobe.com/technologies/flashplayer10/
mike Chambers
[电子邮件受保护]
It sounds like you may be running into a garbage collection issue with the flash player.
A new API has been added to Flash Player 10 that should address this:
unloadAndStop()
Grant Skinner has more info on this on his blog:
http://www.gskinner.com/blog/archives/2008/07/unloadandstop_i.html
You can grab a beta of Flash Player 10 at:
http://labs.adobe.com/technologies/flashplayer10/
mike chambers
[email protected]
要完全删除 AS3 中的对象,必须将其值设置为 null。 垃圾收集将毫无问题地删除它,因为没有对它的引用。 另外,如果对事件侦听器使用“弱引用”会有所帮助。 创建事件侦听器时,通常是事件类型和要触发的函数。
下面我将说明相同的内容,但参考较弱。
我们知道前两个参数是什么,所以让我们从第三个参数开始。 第三个参数指示事件是在捕获阶段(true)还是冒泡阶段(false,这也是默认值)触发 onEventHappened 函数。 我提到此参数的唯一原因是在设置弱引用参数之前需要它。 第四个参数是优先级,指示在侦听同一对象和事件流的同一阶段时哪些事件具有优先级。 第五个参数将弱引用设置为 true 或 false,在这种情况下我们将使用 true,这有助于垃圾回收。
To completely get rid of an object in AS3 you must set its value to null. Garbage collection will have no problems removing it because there are no references to it. Also if can be helpful to use "weak references" with event listeners. When creating an event listener it typically is the event type and the function to be fired.
below I will illustrate the same, but with a weak reference.
We know what the first two parameters are so lets begin with the third. The third parameter dictates whether the event fires the onEventHappened function during the capture phase (true) or the bubbling phase (false which is also the default). The only reason I am mentioning this parameter is that it is required prior to setting the weak reference parameter. The fourth parameter is priority and dictates which events have priority when both listening on the same object and same phase of the event flow. The fifth parameter sets the weak reference to true or false, for this case we will use true which is helpful for garbage collection.
所涉及的对象是 MovieClip,它是否有正在播放的时间线?
如果是这样,您需要在删除之前停止它。
另请记住,以任何方式存储对对象的引用(尽管最常见的是在事件侦听器中)都会阻止其被垃圾收集。 这包括对函数或子对象的任何引用。
Is the object in question a MovieClip, and does it have a timeline playing?
If so you will need to stop it before removing.
Also keep in mind that storing a reference to the object in any way (although most commonly in an Event listener) will keep it from getting garbage collected. This includes any references to functions or child objects.
对于要调用的函数,根据定义必须有一个侦听器或 setTimeOut 某处,或者时间线必须正在播放。 确保删除所有侦听器以及对该对象的所有引用。 它是什么样的物体?
输出窗口或调试器应该向您显示导致不需要的调用的函数调用堆栈。 如果您将错误输出粘贴到您的问题中,那么我们将能够为您提供更准确的答案。
For a function to be called, by definition there must be either a listener or setTimeOut somewhere, or the timeline must be playing. Make sure you remove all listeners and all references to the object. What kind of object is it?
The output window or debugger should show you the stack of function calls that led to the unwanted call. If you paste the error output into your question then we will be able to give you a more accurate answer.
另请记住在处置已删除的对象时停止并删除任何相关的计时器:BIT-101 :正在运行的计时器不会被垃圾收集。 曾经。
Also remember to stop and remove any related Timers when disposing of the removed objects: BIT-101: Running timers are not garbage collected. Ever.
我会查看 Event.ENTER_FRAME 和 TimerEvent.TIMER 侦听器,确保它们在删除对象之前已无效。
I would look at Event.ENTER_FRAME and TimerEvent.TIMER listeners, make sure they're nullified before you remove the object.
您需要确保要删除的显示对象:
Event.ENTER_FRAME
事件的侦听器设置的计时器调用>setInterval
任何地方所以当您使用
removeChild< 删除对象时/code> 并将其从您保存的数组中删除,同时调用其
stop
方法以确保它没有播放其时间线。 在该对象上有一个名为halt
、cleanup
或finalize
的方法来注销任何侦听器、停止计时器也可能是一件好事、超时、间隔等,清除对其父级、舞台或任何不会消失的对象的引用(即将变量设置为null
)。You need to make sure that the display object you're removing:
stage.removeEventListener(...)
for any correspondingstage.addEventListener(...)
Event.ENTER_FRAME
eventsetInterval
anywhereSo when you have removed the object with
removeChild
and removed it from the array you kept it in, also call itsstop
method to make sure it's not playing its timeline. It may also be a good thing to have a method on that object called something likehalt
,cleanup
orfinalize
that unregisters any listeners, stops timers, timeouts, intervals, etc., clears references (i.e. sets the variables tonull
) to it's parent, the stage or any object that isn't going away too.