Ross,

I'm no expert but I think this is correct:
As long as your object is decended from the EventDispatcher class (all display objects) you can attach a listener to it. Let's say you have a Text field called myObject in another file/class then you can specify the event name, as explained, with something like:

myObject.addEventListener(MyEvent.MYEVENT2, onMyEvent2)

public function onMyEvent2(event:MyEvent):void
{
        Trace("myEvent2 heard!");
}

I used to have problems specifying what to listen for, i.e. the event's name. The MYEVENT2 above is actually a string but you are supposed to use this capitalized version.
The string is defined in the MyEvent class as
public var static const MYEVENT2:String = "SomeNameIdentifier";

Jason's example has "import events.MyEvent;" and so any public static variablesin MyEvent.as are visible. If the MYEVENT2 string is defined as "var public static" in the a class, it is visible to other files that import the class. If it had been defined without the "static" keyword then the variable would belong to instances of the class, not the class, and would not be visible outside of the file.

I liked Jason's lamp and switch example and would like to know more about using it more effectively in design patterns.

John

I hope I am not being a pain about the threads being kept sorted, but this information resource you are creating is really precious.

Lehr, Ross (N-SGIS) wrote:
Thanks Jason... Let me ask one question to try to clarify this for me.  If I 
have the dispatchEvent that broadcasts the URL of my HTLMLoader in a 
onLoadComplete function and I want to listen for that event in a different 
class (for example I want to put the URL in a textbox that is created in a 
different class than the HTMLLoader) where would I put the addEventListener?  
I'm not sure why my head is not wrapping around this, but thanks for all your 
help.

Ross

-----Original Message-----
From: [email protected] 
[mailto:[email protected]] On Behalf Of Merrill, Jason
Sent: Thursday, December 18, 2008 5:20 PM
To: Flash Coders List
Subject: RE: [Flashcoders] Custom eventListener

To add on to David's fine reply, Ross, you're close, but not quite - this is 
how I write custom events, I think you'll want to do something like this 
instead - this works great for me:

To write the event:

package events
{
        import flash.events.Event;

        public class MyEvent extends Event
        {
public static var MYEVENT1:String = "myEvent1"; public static var MYEVENT2:String = "myEvent2";
                
public function MyEvent(type:String, bubbles:Boolean = false, cancellable:Boolean = false ) {
                        super(type, bubbles, cancelable);
                }
                
        }
        
}

Then to dispatch:

import events.MyEvent;

dispatchEvent(new MyEvent(MyEvent.MYEVENT2, false, true));


Then to listen and react to dispatch:

myObject.addEventListener(MyEvent.MYEVENT2, onMyEvent2)

public function onMyEvent2(event:MyEvent):void
{
        Trace("myEvent2 heard!");
}


Jason Merrill
Bank of America     Instructional Technology & Media   ยท   GCIB & Staff Support 
L&LD

Interested in Flash Platform technologies? Join the Bank of America Flash Platform Developer Community Interested in innovative ideas in Learning? Check out the Innovative Learning Blog and subscribe.





-----Original Message-----
From: [email protected] 
[mailto:[email protected]] On Behalf Of David 
Hershberger
Sent: Thursday, December 18, 2008 4:58 PM
To: Flash Coders List
Subject: Re: [Flashcoders] Custom eventListener

An event is just a message object.  Just because one gets created
successfully does not mean it is being delivered.  dispatchEvent() is a
method of the EventDispatcher class, which is a superclass of many classes,
including DisplayObject.  You need to listen for the event (with
addEventListener()) on the same EventDispatcher where the event was
dispatched. (Except in the case of events that bubble, but that's another
topic.)

For example:

public class MyWindowHolder extends Sprite {
  public function init(): void {
    var a: MyWindow = new MyWindow();
    a.addEventListener( MyEvent.EXCLAMATION, onExclamation );
  }

  private function onExclamation( event: MyEvent ): void {
    trace("ouch!");
  }
}

public class MyWindow extends Sprite {
  private function injury(): void {
    this.dispatchEvent( new MyEvent( MyEvent.EXCLAMATION ));
  }
}

Hope this helps,
Dave

On 12/18/08, Lehr, Ross (N-SGIS) <[email protected]> wrote:
Hey All...

     I'm having trouble with a custom event listener.  I'm working on
experiment browser project trying to learn as3, OOP, and AIR.  I
broadcasting the URL after a web page is loaded and I need a text box to
listen for it.  I've added a trace to me urlLoadedEvent class and I know
the URL is getting there (so, I think it's getting broadcasted ok), but
it's not getting to the text box.  I think I have my listener in the
wrong place, but not sure where to put it.  I don't have my exact code
here, but below are some code snippets.  Thanks for any and all help.

Ross



------ DISPACTING  EVEN

In a loadComplete function urlLocation = text field for the URL string

        dispatchEvent(new urlLoadEvent(urlLocation.text));

------ urlLoadEvent Class (when I trace urlLocation I am getting the URL
string)

package
{
        import flash.events.*;

        public class urlLoadEvent extends Event
        {
                public static const URL_CHANGED = "urlChanged";
                public var urlLocation:String;

                public function urlLoadEvent(urlL:String):void
                {
                        super(URL_CHANGED);
                        urlLocation = urlL;
                        trace(urlLocation);
                }

        }
}

------ I'm not sure where to attach this.

???????.addEventListener(urlLoadEvent.URL_CHANGED, onURLChange);
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

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

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

_______________________________________________
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