6 Replies Latest reply on Jan 12, 2007 1:00 PM by tlc

    listen to event

    tlc Level 1
      i have a class that extend nothing, is it possible to have this class listening to Events?
      i found no way to do this, unless i pass some Object(like DisplayObject) into its constructor.

      quote:

      public class myClass{
      public MyClass(){
      addEventListener(Event.ENTER_FRAME, onEnterFrame);
      }
      private function onEnterFrame(event:Event):void{...}
      }


      thankyou
        • 1. Re: listen to event
          buabco Level 1
          Hi,

          You can have a class to listen to events produced by other classes, but if you want your own class to generate events, you need to extend EventDispatcher class.

          BTW, only DisplayObject and it's descendants can produce the ENTERFRAME event.
          • 2. Re: listen to event
            tlc Level 1
            Hi, thankyou for reply

            my problem is: my classs never "add to stage" by addChild command.
            i want it to start monitor to the event after it created

            var myObj:myClass = new MyClass();

            but it seem have no way to monitor to the events, if the object not add to stage.
            and there also not _root anymore.
            i cant use _root to boardcast the events to my class in my class constructor like below:

            function myClass(){
            _root.addEventListener(...)
            }


            my way now is:

            var myObj:myClass = new myClass(this.stage);// where "this" is DisplayObject which already added to stage

            function myClass(stage:Stage){
            stage.addEventListener(...);
            }


            But my idea is, no required to pass reference to the constructor.
            so that my user can create my class in anywhere anyway.

            is it possible?


            thankyou
            • 3. Re: listen to event
              buabco Level 1
              The EventDispatcher on actionscript 3 has nothing to do with the class been or not part of the stage, this is that events can be fired and listened to even though your class is not part of the stage. The ENTERFRAME event specifically it is fired by DisplayObjects beacause its a display related event, but you can generate your own events with your own event class also so they will carry whatever information you need.

              If you want to listen to global stage events, you need a desendant from the DisplayObject class and you need it to be part of the stage, this part you can monitor with the ADDED event witch will tell you when the class has been added to the stage and the "root" and "stage" variables are not null.

              Also have in mind that GLOBAL variables are no longer available in as3, now you have to use the static class definition for that (Witch is a lot cleaner than the old global variables).

              The DisplayObject will broadcast a ENTERFRAME event even though it's not on the stage.

              If you want an event to be fire every XXX seconds or something like that you still gona need to extend the EventDispatcher object and use a Timer Object to fire the event.

              Your function would look like this:

              public class myClass extends EventDispatcher {

              public function myClass() {
              var myTimer:Timer = new Timer(1000, 2);
              myTimer.addEventListener("timer", timerHandler);
              myTimer.start();
              }

              public function timerHandler(event:TimerEvent):void {
              dispatchEvent(new Event("myEvent"));
              }
              }
              }

              • 4. Re: listen to event
                tlc Level 1
                thankyou buabco

                now it work:
                public class ticker{
                public function ticker(){
                var s:Sprite = new Sprite();
                s.addEventListener(Event.ENTER_FRAME, onEnterFrame);
                ...


                how about the KEY_DOWN event;
                it only working when i extends the DisplayObject and listen to ADDED event and add the KEY_DOWN listener once the ADDED event fired.
                this mean my class should add to stage.
                also, the KEY_DOWN event not fire if my myClass object lost focus


                thankyou very much
                • 5. Re: listen to event
                  buabco Level 1
                  Tlc,

                  The Key Events as also the Mouse Events are related to what you are listening from, that's why you lost contact with the event when you loose the focus or in case of mouse events when you leave the DisplayObject's area.

                  Since when you capture the added event you should have a not null stage variable in your DisplayObject (sometimes this is not the case so I've been using the enterFrame event to check for this), you can add a listener to the stage.

                  If you do that though remember to add a REMOVED event listener to your class, so you will remove the stage event listener when the DisplayObject is removed from the stage. This is important or your class wont be destroyed by the garbage collector.
                  • 6. Re: listen to event
                    tlc Level 1
                    thanks again

                    how you checking the key press in enterframe event?
                    i found no way to do that, because the isDown() are removed.