panthro
panthro

Reputation: 24061

Startdrag is not a function

this.addEventListener(MouseEvent.MOUSE_DOWN,function(e:MouseEvent){this.startDrag(false,null);});

Hi I was wondering why the above doesnt work? Im trying to drag a sprite around screen.

Upvotes: 0

Views: 646

Answers (2)

Mark Knol
Mark Knol

Reputation: 10163

If you remove this.; it will work. It's a scope issue since you use an anonymous function. You could use the currentTarget of the event, this allows you to make other boxes draggable too, if you add the same listeners.

Note: It is hard to remove an anonymous function as event listener and could cause memory leaks, so the best way is to use a reference to a named function:

box.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseEvent);
box.addEventListener(MouseEvent.MOUSE_UP, handleMouseEvent);

function handleMouseEvent(event:MouseEvent):void 
{
    switch(event.type)
    {
       case MouseEvent.MOUSE_DOWN:
       {
         DisplayObject(event.currentTarget).startDrag();
         break;
       }
       case MouseEvent.MOUSE_UP:
       {
         DisplayObject(event.currentTarget).stopDrag();
         break;
       }
   }
}

Upvotes: 0

Neil
Neil

Reputation: 8111

create a sprite on stage, add instance name box, add code to frame one:

box.addEventListener(MouseEvent.MOUSE_DOWN, startMove);

function startMove(evt:MouseEvent):void {
   box.startDrag();
}

box.addEventListener(MouseEvent.MOUSE_UP, stopMove);

function stopMove(e:MouseEvent):void {
   box.stopDrag();
}

I think your example doesn't work because of the scope of "this" in the event listener handler.

Upvotes: 1

Related Questions