Accessing event.target or event.currentTarget beyond the listener Function

对着背影说爱祢 提交于 2019-12-13 05:38:59

问题


Originally I had two functions, the first function being dataLoaded which had an eventListener in it that spawned a new function called itemClicked. But if I wanted to add a delayedFunction and have itemClicked as an intermittent stage between dataLoaded and delayedFunction, how can I access the event.target or event.currentTarget of the original listener when I come to reference it in my delayedFunction?

private function dataLoaded(event:Event):void {
    //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.text = items[i].toString();
        // adds textfield to displaylist
        _item.addChild(_itemTextField);
        //adds items to container displaylist
        _container.addChild(_item);
    }
    _item.parent.addEventListener( MouseEvent.CLICK, itemClicked );
}

function itemClicked(event:MouseEvent):void {
    if (event.target is Item) {
        var item:Item = Item(event.target);
        TweenLite.to(item.btn_delete, 1,{rotation:180});
        setTimeout(delayedFunction,5000);
        item.parent.removeChild(item);
        parseXML(data);
    }
}

function delayedFunction():void {
    var item:Item = Item(event.target);
    item.parent.removeChild(item);
}

(I've mocked up the functions above, so I may be missing some closing parenthesis, but this doesn't matter in relation to my question)

This question has been on my mind for days and I can't think of a way of making the event.target of the parent _item accessible to functions other than the listening function.


回答1:


You can pass arguments to your setTimeout call like this:

setTimeout(delayedFunction, 5000, event.currentTarget);

Then get the argument from the arguments array from your delayedFunction method:

function delayedFunction():void {
    var item:Item = arguments[0] as Item;
    item.parent.removeChild(item);
}

Have a look at the setTimeout docs if you need information.



来源:https://stackoverflow.com/questions/16137620/accessing-event-target-or-event-currenttarget-beyond-the-listener-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!