OK. I’ve got this drag and drop working but when I drop it anywhere other thatn the target, such as the stage, I get this error.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
The problem is that it depends on how I drop the answer. If the mouse is directly over the target, everything is fine. But if the mouse is off the target, the drop is considered a miss. Is there a way to avoid this?
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.utils.Dictionary;
var targetArray:Array = [];
stop();
/************* Set up the questions with text from the XML file. *********************/
for(var i:int = 0; i < xmlFile.questions.question.length(); i++)
{
var quesBack:mcQuesBack = new mcQuesBack();
quesBack.x = 30;
quesBack.y = 30 + (i * 80);
quesBack.name = String(i);
quesBack.mouseChildren = false;
quesBack.label = xmlFile.questions.question*.answer;
quesBack.quesText.text = xmlFile.questions.question*.content;
stage.addChild(quesBack);
targetArray* = quesBack;
}
/************* Set up the draggers with text from the XML file. *********************/
for(var n:int = 0; n < xmlFile.draggers.answer.length(); n++)
{
var ansBacking:mcDragger = new mcDragger();
ansBacking.x = 650;
ansBacking.y = 30 + (n * 70);
ansBacking.buttonMode = true;
ansBacking.txtAnswer.text = xmlFile.draggers.answer[n].content;
ansBacking.mouseChildren = false;
ansBacking.name = xmlFile.draggers.answer[n].content;
ansBacking.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
//ansBacking.addEventListener(MouseEvent.ROLL_OVER, handleAlphaOver);
stage.addChild(ansBacking);
}
function startDragging(evt:MouseEvent):void
{
evt.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN, startDragging);
//evt.target.addEventListener(Event.ENTER_FRAME, checkHitTest);
origLocatX = evt.currentTarget.x;
origLocatY = evt.currentTarget.y;
evt.currentTarget.startDrag(false);
evt.currentTarget.parent.addChild(evt.currentTarget);
evt.currentTarget.addEventListener(MouseEvent.MOUSE_UP, onDrop);
}
function onDrop(evt:MouseEvent):void
{
evt.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, onDrop);
evt.currentTarget.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
evt.currentTarget.stopDrag();
if(xmlFile.draggers.@multiUse == true)
{
evt.currentTarget.x = origLocatX;
evt.currentTarget.y = origLocatY;
}
else
{
stage.removeChild(DisplayObject(evt.target));
}
var indexOfCurrentTarget:int = targetArray.indexOf(evt.currentTarget.dropTarget.parent);
if(evt.currentTarget.dropTarget.hitTestObject(targetArray[indexOfCurrentTarget]))
{
targetArray[indexOfCurrentTarget].quesText.text += " " + evt.currentTarget.txtAnswer.text;
}
}