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

前端 未结 6 1889
执念已碎
执念已碎 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:26

    You really need to extend the event class to create your own event with extra parameters. Placing functions inside the addEventListener (anonymous functions) is a recipe for memory leaks, which is not good. Take a look at the following.

    import flash.events.Event;
    
    //custom event class to enable the passing of strings along with the actual event
    public class TestEvent extends Event
    {
        public static const TYPE1 :String = "type1";
        public static const TYPE2 :String = "type2";
        public static const TYPE3 :String = "type3";
    
        public var parameterText:String;
    
        public function TestEvent (type:String, searchText:String)
        {
            this.parameterText = searchText;
            super(type);
        }
    
    }
    

    when you create a new event such as

    dispatchEvent(new TestEvent(TestEvent.TYPE1, 'thisIsTheParameterText'))" ;
    

    you can then listen for that event like this

    someComponent.addEventListener(TestEvent.TYPE1, listenerFunction, true , 0, true);
    

    and inside the function 'listenerFunction' event.parameterText will contain your parameter.

    so inside your myClips component you would fire off the custom event and listen for that event and not the Click event.

提交回复
热议问题