Skip to main content
December 9, 2009
Answered

Trouble with dropTarget

  • December 9, 2009
  • 1 reply
  • 996 views

Good evening AS3 programmers,

I've got another newbie question.  I'm building a FLA with a Drag & Drop function.  I can Drag and if I Drop on the target all is well.  However, if I dropoutside the target, I get the following error message: "TypeError: Error #1009: Cannot access a property or method of a null object reference.
at EvalStat/drop()
"  The target is a MovieClip, DropZone, with an instance name of DropZ.  It was placed on the Stage at design time.

function drop(e:MouseEvent) {
   e.target.stopDrag();
   if (e.target.dropTarget.parent == DropZ) {
      e.target.y = DropZ.y + 10;
      e.target.filters = null;
   } else {
      e.target.Die();
   }
  }

This is a copy of my drop() method.  I created a newbie solution by using e.target.x and e.target.y to test if the drop was within the target area.  It worked, but I'd really like to learn about dropTarget.  Any suggestions would be greatly appreciated.

PS I observed that some threads have code nicely formatted in window.  How is this done?

This topic has been closed for replies.
Correct answer kglad

use:


function drop(e:MouseEvent) {
   e.target.stopDrag();

if(e.target.dropTarget!=null){
   if (e.target.dropTarget.parent == DropZ) {
      e.target.y = DropZ.y + 10;
      e.target.filters = null;
   } else {
      e.target.Die();
   }
  } else {

e.target.Die();  // i think you'll want this

}

}


1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
December 9, 2009

use:


function drop(e:MouseEvent) {
   e.target.stopDrag();

if(e.target.dropTarget!=null){
   if (e.target.dropTarget.parent == DropZ) {
      e.target.y = DropZ.y + 10;
      e.target.filters = null;
   } else {
      e.target.Die();
   }
  } else {

e.target.Die();  // i think you'll want this

}

}


December 10, 2009

kglad,

Once again thanks, you nailed that problem!  I thought that (e.dropTarget.parent == DropZ) would produce either True or False, but it produces null if the item is dropped on to an empty stage.  I cleaned up the code a bit it now looks like this:

  function drop(e:MouseEvent) {
   e.target.stopDrag();
   if (e.target.dropTarget == null || e.target.dropTarget.parent != DropZ) {
    e.target.Die();
   } else {
    e.target.y = DropZ.y + 10;
    e.target.filters = null;
   }
  }

kglad
Community Expert
Community Expert
December 10, 2009

you're welcome.