简体   繁体   English

AS3如何永久删除对象?

[英]AS3 How to delete an object for good?

I create a new Shape, make a listener that starts a function when an object hits the shape, the function clears graphics of the shape, removes listener and deletes the child. 我创建一个新的Shape,创建一个侦听器,当一个对象触碰该形状时,该侦听器将启动一个函数,该函数将清除形状的图形,删除侦听器并删除子级。 But it looks like it leaves some ghost that still triggers when my object comes to it's place. 但是看起来好像留下了一些幽灵,当我的物体到达它的位置时,它仍然会触发。 I'm not sure why it happens, I thought clear+remove should make it impossible to hitTest. 我不确定为什么会发生这种情况,我认为清除+删除应该无法进行hitTest。 But somehow children still pile over one another and the last created one triggers the function. 但是以某种方式,孩子们仍然会彼此叠堆,最后创建的孩子会触发该函数。 So if you understand, please give me a hint, if not please tell me in general what is a way to delete a created Shape for good, so it didn't count existing? 因此,如果您理解,请给我一个提示,如果不能,请总体上告诉我什么是永久删除创建的Shape的方法,这样就不算现有的?

A bit of details. 一点细节。 Hitting the shape must start a function that deletes it and creates a new shape of the same name that has a different function that does the same etc. so the current function must be the one I called for, not the last one, whose listener I deleted. 击中该形状必须启动一个删除该形状的函数,并创建一个具有相同名称的新形状,该形状具有执行相同操作等的不同功能。因此,当前函数必须是我所要求的监听器,而不是我所要求的最后一个已删除。 Making new shape each time may overload memory, besides I do clean up every time. 每次制作新形状都可能使内存超载,此外我每次都要清理。 Did I forget anything? 我忘记了什么吗?

fun0(){
  var bob:Shape = new Shape();
  addChild(bob);
  bob...drawRect...
  addEventListener(...,fun1)
  function fun1(){ if(hitTest...){
  bob...clear();
  removeChild(bob);
  removeEventListener(...,fun1)
  fun2();
  } 
 }


fun2(){
  var bob:Shape = new Shape();
  addChild(bob);
  bob...drawRect...
  addEventListener(...,fun3)
  function fun1(){ if(hitTest...){
  bob...clear();
  removeChild(bob);
  removeEventListener(...,fun3)
  fun0();
  } 
}

etc., it only sees fun3 after I once trigger it even though I deleted the listener. 等等,即使我删除了侦听器,也只有在我一次触发它之后才能看到fun3。 Again, I do move object away, it doesn't hitTest anymore, then clear+remove. 再一次,我确实将对象移开了,它不再点击测试,然后清除并删除。 If you don't understand or cannot help, please don't flame, just ignore this, I need help, not the info that I'm not as smart as you are. 如果您不理解或无法提供帮助,请不要着火,请忽略此信息,我需要帮助,而不是我不像您那么聪明的信息。 Thank you. 谢谢。

first of dont declare functions inside of functions. 首先不要在函数内部声明函数。 then use weak reference for event listners. 然后将弱引用用于事件列表器。

// params: eventName, listener, capturePhase, priority, useWeakReference
someObj.addEventListener("eventName",myFunct,false,0,true);

you musst get rid of any references to the object, then set it to null 您必须摆脱对该对象的任何引用,然后将其设置为null

removeChild(bob); 
bob= null;

after that GC will collect it! 之后,GC将收集它!

edit: 编辑:

if you keep doing the hitTest, like on enterFrame for example. 如果您继续执行hitTest,例如在enterFrame上。 check if the object is on the stage, by checking its .stage property. 通过检查其.stage属性来检查对象是否在舞台上。

if(bob.stage)
  {
    if(hitTest ...){};
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM