How to create custom MouseEvent.CLICK event in AS3 (pass parameters to function)?

前端 未结 6 1896
执念已碎
执念已碎 2021-01-05 05:50

This question doesn\'t relate only to MouseEvent.CLICK event type but to all event types that already exist in AS3. I read a lot about custom events but until now I couldn\'

6条回答
  •  攒了一身酷
    2021-01-05 06:17

    You can accomplish this by getting your handler out of a function that gives the variable closure, like this:

    for (var i=0; i<5; i++) {
        myClips[i].addEventListener( MouseEvent.CLICK, getHandler(i) );
    }
    
    function getHandler(i) {
        return function( e:MouseEvent ) {
            test(i);
        }
    }
    
    function test( j ) {
        trace("foo "+j);
    }
    

    Also, as for why this creates a new closure, you might want to check the explanation in the accepted answer to this similar question.

提交回复
热议问题