Skip to main content
Inspiring
December 19, 2007
Question

AS3 - removeEventListener

  • December 19, 2007
  • 1 reply
  • 2255 views

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
This topic has been closed for replies.

1 reply

December 20, 2007
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