5 Replies Latest reply on Jan 24, 2012 2:30 PM by kglad

    override addEventListner

    DaveGallant Level 1

      I have a sprite class and I want to override the addEventListener and removeEventListener so I can do listener obect tracking in my class, but I keep getting an error.

       

      Here is a stripped down version of my class:

       

      package com.DaveLibrary

      {

          import flash.display.Sprite;

       

          public class clsButton extends Sprite

          {

              public var EventListeners:Vector.<Array>;

       

              public function clsButton():void

              {

                  EventListeners = new Vector.<Array>;

              }

       

              public override function addEventListener(type:String, listener:Function, useCapture:Boolean, priority:int = 0, useWeakReference:Boolean = true):void

              {

                  super.addEventListener(type, listener, useCapture, priority, useWeakReference);

                 

                  var i:int;

                  for(i = 0; i < EventListeners.length; i++)

                  {

                      if(EventListeners[i][0] == type && EventListeners[i][1] == listener) return;

                  }

                  EventListeners.push([type, listener]);

              }

             

              public override function removeEventListener(type:String, listener:Function):void

              {

                  super.removeEventListener(type, listener);

                  var i:int;

                  for(i = 0; i < EventListeners.length; i++)

                  {

                      if(EventListeners[i][0] == type && EventListeners[i][1] == listener)

                      {

                          EventListeners.splice(i,1);

                          i--;

                      }

                  }

              }

             

              public function removeAllEventListeners():void

              {

                  while(EventListeners.length > 0)

                  {

                      super.removeEventListener(EventListeners[0][0], EventListeners[0][1]);

                      EventListeners.splice(0,1);

                  }

              }

          }

      }

       

       

      When I compile, I get this:

       

      clsButton.as, Line 7121023: Incompatible override.

       

      line 712 is my public override function addEventListener line.

       

       

      anyone know what I'm doing wrong?

        • 1. Re: override addEventListner
          kglad Adobe Community Professional & MVP

          you must use the same method signature as the overriden method.  try:

           

           

           

          package com.DaveLibrary

          {

              import flash.display.Sprite;

           

              public class clsButton extends Sprite

              {

                  public var EventListeners:Vector.<Array>;

           

                  public function clsButton():void

                  {

                      EventListeners = new Vector.<Array>;

                  }

           

                  public override function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int = 0, useWeakReference:Boolean = true):void

                  {

                      super.addEventListener(type, listener, useCapture, priority, useWeakReference);

           

                      var i:int;

                      for(i = 0; i < EventListeners.length; i++)

                      {

                          if(EventListeners[i][0] == type && EventListeners[i][1] == listener) return;

                      }

                      EventListeners.push([type, listener]);

                  }

           

                  public override function removeEventListener(type:String, listener:Function,useCapture:Boolean=false):void

                  {

                      super.removeEventListener(type, listener);

                      var i:int;

                      for(i = 0; i < EventListeners.length; i++)

                      {

                          if(EventListeners[i][0] == type && EventListeners[i][1] == listener)

                          {

                              EventListeners.splice(i,1);

                              i--;

                          }

                      }

                  }

           

                  public function removeAllEventListeners():void

                  {

                      while(EventListeners.length > 0)

                      {

                          super.removeEventListener(EventListeners[0][0], EventListeners[0][1]);

                          EventListeners.splice(0,1);

                      }

                  }

              }

          }

           

          • 2. Re: override addEventListner
            DaveGallant Level 1

            FFS!

             

            I missed the =false on the useCapture param.

             

            cripes... i feel like a ripe idiot now... I've been starring at that for days.

             

            Thanks bud.

            • 3. Re: override addEventListner
              kglad Adobe Community Professional & MVP

              you're welcome.

               

              p.s.  you also forgot the useCapture parameter in removeEventListener().

              • 4. Re: override addEventListner
                DaveGallant Level 1

                Yeah, I saw... I changed it in both and now it works awesome... I can finally know what handlers are added to my object.

                I am surprised Adobe hasn't added that internally.

                 

                 

                Meh... sit feel like an idiot. LOL

                • 5. Re: override addEventListner
                  kglad Adobe Community Professional & MVP

                  i've made a lot of dumber errors than that, by far.  and some dumb errors i've made more than once at different times.

                   

                  heck, i don't even consider that a dumb error.  it's a subtle point because noone makes a big deal about what a method "signature" is.