0

我在滚动列表中有一个项目列表,我希望用户单击特定项目以将其删除-我需要将 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);            
        }

不幸的是,我之前的努力只成功删除了列表中的最后一项。如何将侦听器应用于所有实例,但在侦听器函数中仅删除已单击的项目?

4

2 回答 2

1

您可以以 evt.target 的形式访问事件侦听器中单击的项目

尝试:

function itemClicked(event:MouseEvent):void {
    if(evt.target is Item) {
        var item:Item = Item(evt.target);
        item.parent.removeChild(item);
    }
}
于 2013-04-21T20:35:33.770 回答
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 个侦听器,因此您只需在完成列表后删除 1。

于 2013-04-21T20:35:57.627 回答