调用removeChild后精灵可见
我有一个装有孩子的容器,需要定期更换。这些孩子是精灵,他们有一个针对 MOUSE_DOWN 事件的弱引用事件侦听器。当我对子项调用removeChild时,容器中的numChildren减少到0,但它们在Flash中仍然可见。我什至尝试删除容器并重新创建新容器,但它仍然不起作用。还尝试添加一些虚拟的简单精灵,而不使用任何事件侦听器,但它没有带来任何结果。我认为垃圾收集器没有按我希望的方式工作:)
这是我用于创建精灵的代码:
itemHolder=new Sprite();
addChild(itemHolder);
itemHolder.y=itemHolderY;
var itemY:Number=0;
for(var i:Number=0;i<modelBarList.bars.length;i++){
var item:bar_list_item;
item=new bar_list_item();
item.name="item"+i;
/*
setting properties for item
*/
itemHolder.addChild(item);
item.y=itemY;
item.mouseChildren=false;
item.buttonMode=true;
itemY+=item.height;
item.addEventListener(MouseEvent.MOUSE_UP,barSelected,false,10,true);
}
以及用于删除的代码:
while(itemHolder.numChildren){
var item:Sprite=Sprite(itemHolder.getChildAt(0));
item.removeEventListener(MouseEvent.MOUSE_UP,barSelected,false);
itemHolder.removeChild(item);
item=null;
}
removeChild(itemHolder);
itemHolder=null;
I have a container with children that needs to be replaced by other periodically. Those children are sprites and they have one weak referenced event listeners for MOUSE_DOWN event. When I call removeChild on children, numChildren from container decreases to 0 but they are still visible in flash. I even tried to remove container and recreate new one but it still doesn't work. Also tried to add some dummy simple sprite instead, without any event listeners but it didn't bring any results. I presume that garbage collector is not working as I wish :)
Here is my code for creating sprites:
itemHolder=new Sprite();
addChild(itemHolder);
itemHolder.y=itemHolderY;
var itemY:Number=0;
for(var i:Number=0;i<modelBarList.bars.length;i++){
var item:bar_list_item;
item=new bar_list_item();
item.name="item"+i;
/*
setting properties for item
*/
itemHolder.addChild(item);
item.y=itemY;
item.mouseChildren=false;
item.buttonMode=true;
itemY+=item.height;
item.addEventListener(MouseEvent.MOUSE_UP,barSelected,false,10,true);
}
And code for removing:
while(itemHolder.numChildren){
var item:Sprite=Sprite(itemHolder.getChildAt(0));
item.removeEventListener(MouseEvent.MOUSE_UP,barSelected,false);
itemHolder.removeChild(item);
item=null;
}
removeChild(itemHolder);
itemHolder=null;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这与垃圾收集器没有任何关系。您是否 100% 确定第二个代码中的 itemHolder 与第一个代码中的 itemHolder 相同?也许您可以尝试将
trace("Yep, this codeexecutes")
添加到第二个代码片段,看看它是否确实执行?我认为,或者问题出在代码的其他地方。I don't think it has anything to do with Garbage Collector. Are you 100% sure that the itemHolder in second code is the same as the itemHolder in the first? Perhaps you could try adding
trace("Yep, this code executes")
to the second snippet and see if it executes for sure? That, or the problem lies somewhere else in your code I think.