4 Replies Latest reply on Jan 18, 2011 6:26 AM by _spoboyle

    event bubbling question

    Robert S Level 1
      I am trying to have a custom class extend the EventDispatcher class to be able to dispatch events. I don't have any problem with instanciating the class then attaching an 'addEventListener()" to it, but I would like for the class to bubble the event where I can add the event listener on the app itself and dispatch the event from WITHIN the class...

      If I create a COMPONENT that extends something (Canas, Panel, Etc), these have no problem with event bubbling. I don't have to actually add the event listener to the component, I can dispatch an event from WITHIN the COMPONENT and it bubbles down to the app.

      My extended EventDispatcher class, however, will not bubble... does anybody know why?

      Any examples of a pure ActionScript class that extends the EventDispatcher and can internally create a "dispatchEvent()" that bubbles down the components that contain it would be of GREAT help.
        • 1. Re: event bubbling question
          bishopx

          Hi Robert S

          Can you post your code and have you enabled bubbling when you dispatch your event example below:

           

          dispatchEvent(new Event(Main.ENTER_SCREEN_COMPLETE, true));

           

          have a look at this as well:

           

          http://dispatchevent.org/mims/reader-question-why-isnt-event-bubbling-working/

           

           

          regards Mike

          • 2. Re: event bubbling question
            _spoboyle Level 4
            <?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"
                              xmlns:local="*"
                              minWidth="955" minHeight="600"
                              creationComplete="init()">
                 
                 <fx:Script>
                      <![CDATA[
                           [Bindbale]
                           private var count:int = 0;
                           
                           private function init():void
                           {
                                addEventListener(MouseEvent.CLICK, onEvent);
                           }
                           
                           private function onEvent(e:Event):void
                           {
                                trace("event receieved from button");
                                count++;
                                counter.text = "Button clicked "+ count +" times.";
                           }
                           
                      ]]>
                 </fx:Script>
                 
                 <fx:Declarations>
                      <!-- Place non-visual elements (e.g., services, value objects) here -->
                 </fx:Declarations>
                 
                 <s:VGroup horizontalCenter="0" verticalCenter="0">
                      <local:MyPanel/>
                      <s:Label id="counter"/>
                 </s:VGroup>
                 
            </s:Application>
            

             

            package
            {
                 import flash.events.Event;
                 import flash.events.MouseEvent;
                 
                 import spark.components.Button;
                 import spark.components.Panel;
                 
                 public class MyPanel extends Panel
                 {
                      public function MyPanel()
                      {
                           super();
                           var button:Button = new Button();
                           addElement(button);
                      }
                 }
            }
            

             

            there you go have you implemented your own custom event class? if so post code here please

            • 3. Re: event bubbling question
              Lee Burrows Level 4

              hi

               

              apologies if i misunderstand, but bubbling only works up (ie: bubbles up through parents) but not down (through children)

              • 4. Re: event bubbling question
                _spoboyle Level 4

                ahh sorry i didn't read your post correctly but yes you are right you cannot bubble events down the displaylist only up it.

                 

                And I don't really understand what you are trying to achieve in order to suggest an alternative. if you could post your code with what you are trying to achieve I could take a look. But you want to detect an event and pass this down through to children of the component detecting the event then the parent will have references to these children and you pass the event that way.