3 Replies Latest reply on Nov 17, 2010 8:16 PM by rck_Cena77

    dispatchevent problem

    rck_Cena77 Level 1

      I am trying to run a basic eventlistener but unable to run, in debugger shows both the init and creationcomplete methods are executed but the showText is  not able to run what might be the reason?

       

      <?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/mx" minWidth="955" minHeight="600"
                     creationComplete="onComplete()"
                     initialize="init()">
          <fx:Script>
              <![CDATA[
                  public static const callMe="Call_Event";

                   public    function onComplete():void{
                      dispatchEvent(new Event(callMe,true,false));
                  }

                 public    function init():void{
                      group.addEventListener(callMe,showText);
                  }

          public function showText(e:Event):void{
                        rt.visible=true;   
                  }
              ]]>
          </fx:Script>
         
          <s:VGroup id="group">
             
         
          <s:RichText 
              id="rt"
              text="Hai"
              visible="false">
         
          </s:RichText>
          </s:VGroup>
      </s:Application>

       

      thanks for reading.

        • 1. Re: dispatchevent problem
          Abhinav Sharma

          Hi, Your dispatched event is not reaching to 'group' so it is not listening it.

           

          You are dispatching it at the top application level.

          So you can listen it at application level by adding listener at application itself rather than on group as this.addEventListener(CALL_ME_EVENT,showText);

           

          or

           

          dispatching event from group container i.e. group.dispatchEvent(new Event(CALL_ME_EVENT,true,false));

           

          I have done it by dispatching event at group level and it works.

           

          <?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/mx"
                         initialize="{init();}"
                         creationComplete="{onComplete();}"
                         minWidth="955" minHeight="600">
              <fx:Declarations>
                  <!-- Place non-visual elements (e.g., services, value objects) here -->
              </fx:Declarations>
              <fx:Script>
                  <![CDATA[
                      import mx.controls.Alert;
                      public static const CALL_ME_EVENT:String="Call_Event";
                     
                      public function init():void
                      {
                          group.addEventListener(CALL_ME_EVENT,showText);
                      }           
                      public function onComplete():void
                      {
                          group.dispatchEvent(new Event(CALL_ME_EVENT,true,false));
                      }
                                 
                      public function showText(e:Event):void
                      {
                          rt.visible=true; 
                          trace("Done.. :)");
                      }
                  ]]>
              </fx:Script>
             
              <s:VGroup id="group">
                  <s:RichText id="rt"    text="Hai" visible="false"/>
              </s:VGroup>

          </s:Application>

           

          Hope it helps you.

           

           

          -

          Abhinav Sharma

          • 2. Re: dispatchevent problem
            saisri2k2 Level 4

            yeh,, mke it group.dispatchEvent

            1 person found this helpful
            • 3. Re: dispatchevent problem
              rck_Cena77 Level 1

              thanks guys for quick reply, this forum is awesome, learning as3....