我有一个滚动列表中的项目列表,我希望用户点击一个特定的项目来删除它 - 我需要将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);
}
不幸的是,我之前的努力只是设法删除了列表中的最后一项。如何将侦听器应用于所有实例,但在侦听器函数中只删除已单击的项?
答案 0 :(得分:1)
您可以在事件监听器中以evt.target
的形式访问所点击的项目尝试:
function itemClicked(event:MouseEvent):void {
if(evt.target is Item) {
var item:Item = Item(evt.target);
item.parent.removeChild(item);
}
}
答案 1 :(得分:1)
你的问题是你所拥有的是一个名为_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。