Search code examples
actionscript-3flashdraggablereferenceerror

AS3 Works but I get a ReferenceError: Error #1069 Property startDrag not found


I am trying to make a simple project when you click a button a draggable MovieClip is added to the stag and when you click it releases the MovieClip to the X/Y where you clicked, you can then pickup the MovieClip and drag it into a bin (MovieClip) where it destroys itself. The code is working great I can make multiple Movieclips with the button and they are all destroyed when I drag them in the bin however I don't like having "Error Codes".

import flash.events.MouseEvent;
var rubbish:my_mc = new my_mc();
btntest.addEventListener(MouseEvent.CLICK, makeRubbish);

function makeRubbish (event:MouseEvent):void {


addChild(rubbish);

rubbish.x = mouseX - 10;
rubbish.y = mouseY - 10;
rubbish.width = 50;
this.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
rubbish.buttonMode = true;
}

function stopDragging (event:MouseEvent):void {
    rubbish.stopDrag()
    event.target.addEventListener(MouseEvent.CLICK, startDragging);
    rubbish.buttonMode = true;
            if (event.target.hitTestObject(bin))
            {
                trace("hit");
                 event.target.name = "rubbish"; 
    removeChild(getChildByName("rubbish"));


            }
}
function startDragging (event:MouseEvent):void {
event.target.startDrag();
this.addEventListener(MouseEvent.CLICK, stopDragging);
}

Solution

  • Some Pointers:

    • The target property of an Event is not always what it seems. It actually refers to the current phase in the event bubbling process. Try using the currentTarget property.
    • I would also recommend tying the stopDragging method to the stage, as sometimes your mouse won't be over the drag as you're clicking.
    • I would use the MOUSE_UP event as opposed to a CLICK for standard dragging behaviour.
    • When dragging, keep a global reference to the drag in order to call the stopDrag method on the correct object.

    Try This:

    import flash.events.MouseEvent;
    
    var rubbish:my_mc = new my_mc();
    var dragging:my_mc;
    
    btntest.addEventListener(MouseEvent.CLICK, makeRubbish);
    
    function makeRubbish (event:MouseEvent):void {
    
      addChild(rubbish);
    
      rubbish.x = mouseX - 10;
      rubbish.y = mouseY - 10;
      rubbish.width = 50;
      rubbish.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
      rubbish.buttonMode = true;
    
    }
    
    function stopDragging (event:MouseEvent):void {
    
      this.stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragging);
    
      if(dragging !== null){
    
        dragging.stopDrag();
    
        if (event.currentTarget.hitTestObject(bin)){
    
          removeChild(dragging);
    
        }
    
        dragging = null;
    
      }
    }
    
    function startDragging (event:MouseEvent):void {
    
      dragging = event.currentTarget as my_mc;
    
      dragging.startDrag();
    
      this.stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
    
    }