AS3 Add Event Listener in For Each
I would like to add an eventlistener to all the elements I create in a for each loop. But apperently the eventlistener only works with th开发者_如何转开发e latest created element.
How do I add an Event Listener to every element created in the for each loop?
This is my code:
for each(var showCase:ShowCaseItem in _bllShowCase.arrShowCase)
{
var listItem:ListItemShowCase = new ListItemShowCase(showCase);
listItem.y = yPos;
listItem.addEventListener("ITEMDELETED", refreshShowCaseItems);
this.addChild(listItem);
yPos += 20;
}
I hope you understand my question.
Thanks, Vincent
There is nothing functionally wrong with this code. It should work.
The problem probably lies somewhere else.
The display list will hold a reference to each object, so the GC shouldn't clean up any of the listeners, but you can try caching each object into an array. This way you are sure there is a reference to each object outside of this code block.
private var _list:Array = new Array();
for each( var item:Object in list )
{
_list.push( listItem );
}
精彩评论