4 Replies Latest reply on Dec 15, 2006 11:17 AM by FlightGuy

    custom event issue

    lenny2
      Hope you flex geniuses can help me on this one....

      the issue: an action script custom event is being dispatched by one object but is not being "heard" by it's parent despite the fact that a) the child object uses the [event] declaration in it's class definition, b) the event is being correctly dispatched, and c) the parent object uses an addEventListener method to listen for the event.

      the layout:
      includes an application component (mxml) that has as it's only direct child an action script custom component called FloorPlan. On creationComplete the floor plan object instantiates an action script class called Booth as many times as is required (based on the number of booths that needs to be displayed on the floor plan) and then adds the instances to itself via addChild() method. The booth objects are based on Sprite class and so they have click events, etc. When one of the booth instances is clicked, the click event of the booth instance fires and the handler creates an instance of the ShowCompanyProfileEvent (action script class based on the Event class) and then dispatches it. The ShowCompanyProfileEvent event handler for now should just open an Alert box.

      I know the code works within the Booth class because when I register an event listener in the booth class itself and then click on a booth, the listener "hears" the dispatched event and opens the Alert box.

      The problem is that The floor plan instance has a registered event listener to listen for the dispatched custom event also but nothing happens when I click a booth instance (which like I said should dispatch the custom event). I deleted the addEventListener code in the booth class thinking that maybe it was catching it first but still it wouldn't work.

      Any ideas? I have reallysearched and searched to no avail. As I mentioned, I get no errors at all when I compile and run the code. I just don't get the expected results.
        • 1. Re: custom event issue
          FlightGuy Level 1
          You need to listen on the object that dispatches the event, not its parent. So listen on each added booth (from the parent), and your listener will be fired.

          If you want your event to bubble (so it gets fired on the ancestors), make sure you set the second parameter of the constructor to true. ie.

          myEvent = new Event("showStuff", true);

          It's not normal to do this for high-level events like the one in your example, but rather for mouse clicks and keyboard events - though it should work if that's what you want to do.
          • 2. Re: custom event issue
            lenny2 Level 1
            I guess I'm confused now. The booth instances are children of the floor plan object. Ultimately what I'm trying to do is when a user clicks on a booth instance, a new CompanyProfile object will display with the company logo and info. So the booth is being clicked on and somehow I have to tell the floor plan object (the booth's parent) that that happened and to create an instance of the CompanyProfile object. The event also had to pass in the booth number property of the selected (cicked on ) booth which is why I had to go with a custom event. If there is a best practices method of getting a parent to perform an action based on an event dispatched or othewise occuring by/on a child, I'm open to suggestions.
            • 3. Re: custom event issue
              lenny2 Level 1
              bubbles was the ticket...geez...4 hours of my life gone. Thanks a ton, FG!
              • 4. Re: custom event issue
                FlightGuy Level 1
                Bubbles was an option, though I wouldn't have used it. What I meant about listening to the child is something along these lines:

                class FloorPlan...{
                public function addBooth(booth:Booth):void{
                addChild(booth);
                booth.addEventListener("showStuff", displayCompanyProfile);
                }

                private function displayCompanyProfile(event:BoothEvent):void{
                // Show your stuff
                }
                }

                Now when clicked, your Booth class will dispatch a non-bubbling event. This event is 'subscribed to' by the parent, and will execute the displayCompanyProfile function when any of the children is clicked.