Sure - so you're writing a class for a simulation of a machine that has three 
latches and a lightbulb. The user pulls latch A,B,C - your class has some logic 
that checks to see if all three latches are pulled - if so, you dispatch a 
custom event.  You have another class which shows a light bulb - and that class 
listens for the machine class to know when all three latches are pulled.  When 
it hears that they are (from your dispatch), it turns the light bulb on (by 
say, telling a movie clip to show the light up state or whatever - or applies a 
glow to the object  for example).  

This is all simplified, advanced coders would use some design patterns or 
frameworks to handle all this, but you get the idea.


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 Cor
Sent: Thursday, December 18, 2008 5:27 PM
To: 'Flash Coders List'
Subject: RE: [Flashcoders] Custom eventListener

Thanks Ross and Jason!

Now I understand this.
But I am trying to imagine when I would use a custom event.
I have no idea?
Could you give an example?

Kind regards
Cor

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Merrill,
Jason
Sent: donderdag 18 december 2008 23:20
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
No virus found in this incoming message.
Checked by AVG - http://www.avg.com 
Version: 8.0.176 / Virus Database: 270.9.19/1855 - Release Date: 18-12-2008
20:06


_______________________________________________
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