我有一个滚动列表中的项目列表,我希望用户单击特定项目以删除它-我需要将eventListener应用于该项目的所有实例,以便单击任何项目,它都被删除
这是第一个函数:
private function dataLoaded(event:Event):void {
// this holds the loaded xml data //
data = new XML(event.target.data);
//items properties call - add other calls to master properties later on//
items = data.item;
// parsing of each ingredient//
for (var i = 0; i < items.length(); i++) {
// instantiation of mcItem (the stage for each item)
_item = new Item();
// sets //over// layer to invisible / transparent //
_item.item_btn_over.alpha = 0;
// creates the var itemTextField //
_itemTextField = new TextField();
// _itemTextField visual attributes //
_itemTextField.x = _textFieldXPosition + _textFieldPaddingLeft;
_itemTextField.y = _textFieldYPosition;
_itemTextField.selectable = true;
_itemTextField.wordWrap = true;
_itemTextField.width = _textFieldWidth;
_itemTextField.height = _textFieldHeight;
_itemTextField.embedFonts = true;
_defaultFormat.color = 0x111112;
_defaultFormat.font = _arialRounded.fontName;
_defaultFormat.size = 18;
_itemTextField.defaultTextFormat = _defaultFormat;
_itemTextField.text = items[i].toString();
//adds textfield to displaylist//
_item.addChild(_itemTextField);
//vertical positioning//
_item.y = i * _itemPosition;
_item.buttonMode = true;
_item.mouseChildren = false;
//adds items to container displaylist//
_container.addChild(_item);
}
// Input Mask//
_mask = new Shape();
_mask.graphics.beginFill(0xFF0000);
_mask.graphics.drawRect(0, 0, _maskWidth, _maskHeight);
_mask.graphics.endFill();
// Positioning of input mask//
// horizontal centering of input mask//
_mask.x = stage.stageWidth / 2 - _container.width / 2;
_mask.y = _paddingTop;
// adds the mask onto the stage//
addChild(_mask);
// assigns the above mask to the container //
_container.mask = _mask;
// Positioning of container with the mask//
// horizontal centering of container //
_container.x = stage.stageWidth / 2 - _container.width / 2;
// vertical position of container //
_container.y = _paddingTop;
//Container background stylings//
_background = new Shape();
_background.graphics.beginFill(0xFFFFFF);
_background.graphics.drawRect(0, 0, _container.width, _container.height);
_background.graphics.endFill();
_container.addChildAt(_background, 0);
//End of container background stylings//
_item.parent.addEventListener( MouseEvent.CLICK, itemClicked );
_container.addEventListener(MouseEvent.MOUSE_OVER, movingOver);
_container.addEventListener(MouseEvent.MOUSE_OUT, movingOut);
}
这是itemClicked
函数:
function itemClicked(event:MouseEvent):void {
_item.parent.removeChild(_item);
}
不幸的是,我之前的努力只设法删除了列表中的最后一项。如何将侦听器应用于所有实例,但在侦听器函数中仅删除已单击的项?
你的问题是,你有什么我假设是一个类成员变量称为_item,当你循环,你重用该变量来创建你的新项目。因此,_item只能是对您创建的最后一个项的引用。
所以当你在itemClickHandler中使用它时——它总是引用你创建的最后一个项目。
下面是你应该做的一个例子:
// this is just example code for your dataLoaded function showing the correct concept
for (var i = 0; i < items.length(); i++)
{
var newItem:Item = new Item;
// do whatever you need to newItem
newItem.addEventListener(MouseEvent.CLICKED, itemClicked);
}
function itemClicked(event:MouseEvent):void
{
var curItem:Item = event.currentTarget as Item;
curItem.parent.removeChild(curItem);
}
尽管你也可以这样做:
// this is just example code for your dataLoaded function showing the correct concept
for (var i = 0; i < items.length(); i++)
{
var newItem:Item = new Item;
// do whatever you need to newItem
}
_container.addEventListener(MouseEvent.CLICKED, itemClicked);
function itemClicked(event:MouseEvent):void
{
// you could also have a check here to see if it is indeed an Item
var curItem:Item = event.target as Item;
curItem.parent.removeChild(curItem);
}
第二种方法的优点是只有一个侦听器,因此您只需要在处理完列表后删除1个侦听器。
您可以像event .target
那样访问事件侦听器中已单击的项试题:
function itemClicked(event:MouseEvent):void {
if(evt.target is Item) {
var item:Item = Item(evt.target);
item.parent.removeChild(item);
}
}