>>. What I have working is the following:
>>Singleton - has private var EventDispatcher
>>EventDispatcher- has private var CustomSprite
>>CustomSprite dispatches CustomEvent.

Yes, and what I am saying is, that architecture is wrong.  You don't
create instances of EventDispatcher and then put Display object inside
of them.  

>> I have to "pass" the event up the ladder to the singleton.  I'd
rather leap over the EventDispatcher.

Right, exactly, so don't use it at all.  

>> What I want is:
>>CustomSprite dispatches CustomEvent
>>Singleton catches CustomEvent.

Right, so just do that. Like this.  First, create your custom event:

//MyCustomEvent.as
package 
{
        import flash.events.Event;

        public class MyCustomEvent extends Event 
        {
                public static const MYEVENT:String = "myevent";
                
                public function MyCustomEvent(type:String,
bubbles:Boolean=false, cancelable:Boolean=false) 
                { 
                        super(type, bubbles, cancelable);
                } 
                
                public override function clone():Event 
                { 
                        return new MyCustomEvent(type, bubbles,
cancelable);
                } 
                
                public override function toString():String 
                { 
                        return formatToString("MyCustomEvent", "type",
"bubbles", "cancelable", "eventPhase"); 
                }       
        }
}


Then, create your custom display object and dispatch that event:

//MyCustomEvent.as
package  
{
        import flash.display.Sprite;

        public class MyCustomDisplayObject extends Sprite
        {
                
                public function MyCustomDisplayObject() 
                {
                        somethingHappens();
                }
                
                private function somethingHappens()
                {
                        dispatchEvent(new
MyCustomEvent(MyCustomEvent.MYEVENT));
                }
                
        }

}

Then, in your class (which in your case happens to be a Singleton class,
(which again, is irrelevant whether it is a Singleton or not), listen to
it:

//MyClass.as
package  
{
        import MyCustomEvent;
        
        public class MyClass extends
        {
                private var
_myCustomDisplayObject:MyCustomDisplayObject;
                
                public function MyClass() 
                {
                        _myCustomDisplayObject = new
MyCustomDisplayObject();
        
_myCustomDisplayObject.addEventListener(MyCustomEvent.MYEVENT,
onMyEvent);
                }
                
                private function onMyEvent(event:MyCustomEvent):void
                {
                        trace("MyCustomEvent fired")
                }
        }
}


For only moving one level up the display object chain, you don't need to
bother with bubbling.  If you go more than one level up, then turn
bubbling on when you dispatch from the MyCustomDisplayObject class:

dispatchEvent(new MyCustomEvent(MyCustomEvent.MYEVENT, true));


Jason Merrill 

Bank of  America  Global Learning 
Learning & Performance Solutions

Join the Bank of America Flash Platform Community  and visit our
Instructional Technology Design Blog
(note: these are for Bank of America employees only)






-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of
Mendelsohn, Michael
Sent: Wednesday, March 17, 2010 2:39 PM
To: Flash Coders List
Subject: RE: [Flashcoders] bubbling listening

Hi Jason, thanks for responding!

> Make sense?
most.  :-)

> What do you mean "The EventDispatcher contains an instance of a
sprite."

The EventDispatcher has a private var that's a reference to a custom
class that extends Sprite.
The Event Dispatcher is instanced in the singleton, it's not on the
stage, it just directs traffic, so to speak.

> Normally what you would do is not create an instance of
EventDispatcher,[Merrill, Jason] t
but instead have your custom class extend Sprite (which already extends
EventDispatcher anyway), have that sprite dispatch a custom event.

Yes, that's happening.

> Then have your class listen to the instance of that class for the
custom
event you dispatch.

Yes, I'm doing that too.

> So in summary, you write an event class, dispatch
that custom event from the "sprite" class (the custom class that extends
sprite) and listen for that event on that instance in your Singleton
(the fact that it's a Singleton should be irrelevant).

This is the part I'm not getting to work. What I have working is the
following:

Singleton
        - has private var EventDispatcher

EventDispatcher
        - has private var CustomSprite

CustomSprite dispatches CustomEvent.

EventDispatcher catches CustomEvent from Sprite.
EventDispatcher dispatches same CustomEvent.  <-- unnecessary step??
Singleton catches CustomEvent from EventDispatcher (passed along from
CustomSprite).

What I want is:
CustomSprite dispatches CustomEvent
Singleton catches CustomEvent.

Within singleton, this does *not* work:
myEventDispatcher[myCustomSprite].addEventListener...

I have to "pass" the event up the ladder to the singleton.  I'd rather
leap over the EventDispatcher.

- Michael M.


_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to