English 中文(简体)
自定义事件调度
原标题:Custom Event Dispatching

我试图从子类中点燃一个事件, 我有3个班级; 事件汉德勒,主要类, 亚类。

< 强势> EvenHandler.as

    public class EventHandler extends Event
{
    public static const TEST_EVENT:String = "Test"; 

    public function EventHandler($type:String, $params:Object, $bubbles:Boolean = false, $cancelable:Boolean = false)
    {
        super($type,$params, $bubbles, $cancelable);
        this.params = $params;
    }

    public override function clone():Event
    {
        return new EventHandler(type, this.params, bubbles, cancelable);
    }

}

< main > mainClass.as

        public function MainClass()
    {
        addEventListener(EventHandler.TEST_EVENT, testFunc);

    }


    private function testFunc(e:EventHandler){
        trace("OK");
    }

<强度 > SubClass.as

private function CustomFunction(event:MouseEvent):void {
        dispatchEvent(new EventHandler(EventHandler.TEST_EVENT,customObject));

    }

I get VerifyError: Error #1063: flash.events::Event() What s wrong with my architect? Thanks!

最佳回答

Remove $params from super($type,$params, $bubbles, $cancelable); Like this:

public class EventHandler extends Event
{
    public static const TEST_EVENT:String = "Test"; 

    public function EventHandler($type:String, $params:Object, $bubbles:Boolean = false, $cancelable:Boolean = false)
    {
        super($type, $bubbles, $cancelable);
        this.params = $params;
    }

    public var params:Object;

    public override function clone():Event
    {
        return new EventHandler(type, this.params, bubbles, cancelable);
    }

}
问题回答

定义自定义事件如 :

  package com.mysite.events 
  {
     import flash.events.Event;

    public class PendingEvent extends Event 
    {
    public var payload:Object = {};
    public static const CONTENT_COMPLETE:String = "contentComplete";

    public function PendingEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false) 
    { 
        super(type, bubbles, cancelable);

    } 

    public override function clone():Event 
    { 
        return new PendingEvent(type, bubbles, cancelable);
    } 


}

}

在别处发送,例如:

    var event:PendingEvent = new PendingEvent(PendingEvent.CONTENT_COMPLETE);
    event.payload.someStuff = "stuff";
    event.payload.moreStuff = "moreStuff"
    dispatchEvent(event);

那就去别的地方听吧

    this.component.addEventListener(PendingEvent.CONTENT_COMPLETE, componentContentComplete);

    private function componentContentComplete(event:PendingEvent):void 
    {
                    // remove listener
        this.component.removeEventListener(PendingEvent.CONTENT_COMPLETE, componentContentComplete);

                    // do something useful with payload
        var payload:Object = event.payload;
                    trace(payload.someStuff); // stuff
                    trace(payload.moreStuff); // moreStuff
    }

<强度 > EDIT:

您不会对有效载荷使用 < code> Object , 创建您选择的另一类类型, 并在该对象上设置道具, 但是您可以理解 。





相关问题
Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

热门标签