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

前端 未结 6 1970
执念已碎
执念已碎 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条回答
  •  Happy的楠姐
    2021-01-05 06:17

    Without knowing more about your application, it seems more like you should use the target to pass parameters, or extend MouseEvent. The former would be more in line with common practice, though. So for example, if you exposed an integer public property on your "clip" object (whatever it is):

    public class MyClip
    {
       public var myPublicProperty:int;
    
       public function MyClip() { //... }
    }
    
    for (var i:int = 0; i < 10; i++)
    {
       myClips[i].myPublicProperty = i;
       myClips[i].addEventListener(MouseEvent.CLICK, doSomething);
    }
    

    ... and then, in your event listener, you could retrieve that property using either the target or currentTarget property of the event (probably currentTarget, in your case):

    function doSomething(event:MouseEvent):void
    {
       trace(event.currentTarget.myPublicProperty.toString());
    }
    

    That ought to do it! Good luck.

提交回复
热议问题