0 Replies Latest reply on Jun 3, 2010 3:13 PM by Cat Pee

    Create Component that Listens for Custom Event

    Cat Pee

      I've read a lot of tutorials and posts in this forum but still seem to be missing something.

       

      I want to be able to create an arbitrary number of instances of a custom class (based on Button) that each listen to a custom event dispatched in the application.  Eventually the event will carry data (including information that will let each Button determine whether it should react to the event), but for now I just want them to be able to hear the event.

       

      What's supposed to happen is

       

      1) Clicking the button labeled "I Make the Button" creates an instance of EventHearingButton using this.addElement.

       

      2) Clicking the button labeled "I Send the Event" dispatches the SuperCustomEvent custom event, with the additional information "I heard that" stored in the Information string

       

      3) The created EventHearingButton hears the event and updates its label to the value in the Information string.

       

      What actually happens is the EventHearingButton is created OK, but nothing happens when the "I Send the Event" button is clicked.

       

      Main.mxml

      <?xml version="1.0" encoding="utf-8"?> 
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                        xmlns:s="library://ns.adobe.com/flex/spark" 
                        xmlns:mx="library://ns.adobe.com/flex/halo"> 
           
      
           <fx:Metadata>
                [Event(name="SuperCustomEvent", type="Classes.SuperCustomEvent")]
           </fx:Metadata>
           
           <fx:Script>
                <![CDATA[
                     import Classes.EventHearingButton;
                     import Classes.SuperCustomEvent;
                     
                     protected function EventDispatcherButton_clickHandler(event:MouseEvent):void
                     {
                          var NewEvent:SuperCustomEvent = new SuperCustomEvent("I heard that");
                          this.dispatchEvent(NewEvent);
                     }
      
                     protected function button1_clickHandler(event:MouseEvent):void
                     {
                          var NewButton:EventHearingButton = new EventHearingButton();
                          NewButton.x=270;
                          NewButton.y=130;
                          this.addElement(NewButton);
                     }
                     
                ]]>
           </fx:Script>
      
           <s:Button x="270" y="90" label="I Send The Event" id="EventDispatcherButton" click="EventDispatcherButton_clickHandler(event)"/>
           <s:Button x="270" y="50" label="I Make the Button" click="button1_clickHandler(event)"/>
      
      </s:Application>
      

       

       

      Classes.EventHearingButton.as

      package Classes
      {
           import spark.components.Button;
           import flash.events.Event;
           import Classes.SuperCustomEvent;
           
           public class EventHearingButton extends Button
           {
                public function EventHearingButton()
                {
                     super();
                     this.label="I haven't heard yet";
                     this.addEventListener(SuperCustomEvent.SUPERCUSTOMEVENT,SuperCustomEventHandler);
                }
                
                private function SuperCustomEventHandler(event:SuperCustomEvent):void {
                     this.label=event.EventInformation;
                }
           }
      }
      

       

      Classes.SuperCustomEvent.as

      package Classes
      {
           import flash.events.Event;
           
           public class SuperCustomEvent extends Event
           {
                public var EventInformation:String;
                public static const SUPERCUSTOMEVENT:String = "SuperCustomEvent";
                
                public function SuperCustomEvent(Information:String)
                {
                     super(SuperCustomEvent.SUPERCUSTOMEVENT, true);
                     this.EventInformation=Information;
                }
                override public function clone():Event{
                     return new SuperCustomEvent(EventInformation);
                }
           }
      }