1 Reply Latest reply on Dec 20, 2007 2:45 AM by dr_ross

    AS3 - removeEventListener

    Level 7

      Hi there.
      Got this code that creates a sprite.
      Need to make it follow the mouse on MOUSE_DOWN and stop on MOUSE_UP.
      Problem is that I can't stop the MOUSE_MOVE Listener given to the stage.
      Any Help...?
      Here is my code:


      import gs.TweenLite;
      import fl.motion.easing.*

      this.stop ();

      function addCircle ( _sp:Sprite, x:uint, y:uint, radius:uint ):void
      {
      _sp.graphics.clear ();
      _sp.graphics.beginFill(0x666666);
      _sp.graphics.drawCircle(x, y, radius);
      _sp.graphics.endFill();
      }

      var sp:Sprite = new Sprite();
      addCircle (sp, 0, 0, 20);
      stage.addChild(sp);

      function moveSprite ( _sp:Sprite ):void
      {
      TweenLite.to(_sp, 1,
      {
      x:mouseX,
      y:mouseY,
      ease:Sine.easeOut
      });
      }

      stage.addEventListener(MouseEvent.MOUSE_DOWN, function ():void {
      moveSprite (sp);
      stage.addEventListener(MouseEvent.MOUSE_MOVE, function ():void {
      moveSprite (sp);
      });
      });

      stage.addEventListener(MouseEvent.MOUSE_UP, function ():void {
      stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveSprite);
      });



      Thanks
        • 1. Re: AS3 - removeEventListener
          dr_ross Level 1
          your stage.addEventListener(MouseEvent.MOUSE_MOVE and stage.removeEventListener(MouseEvent.MOUSE_MOVE need to reference the same function as their second parameter.

          In this example i've made a function called "onMouseMove" but you can call it anything that they both reference.

          stage.addEventListener(MouseEvent.MOUSE_DOWN, function ():void {
          moveSprite (sp);
          stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
          });

          function onMouseMove(event)
          {
          moveSprite (sp)
          }

          stage.addEventListener(MouseEvent.MOUSE_UP, function ():void {
          stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
          });

          Hope that helped, DrRoss