This question is still open, no viable answer has been found as far as I'm aware. I've tested the only current answers with no result. If you do plan on answering I ask that you first look at the solutions proposed, as I cannot get it to work.
update: Ok I think I'm onto this one, the only block that does not send the error is the block at the very bottom of the checkforhit function, till trying to figure out what that means.
Cannot access a property or method of a null object reference.
at impossible_fla::MainTimeline/checkForHit()
so I recently created a project with a timer on one frame, but for some reason the remove event listener isn't working.
Here is the error text TypeError: Error #1009: Cannot access a property or method of a null object reference. at impossible_fla::MainTimeline/checkForHit()
Even though I did removeEventListener(Event.ENTER_FRAME, checkForHit); ?
stop();
var currentObject:MovieClip = null;
var dragging:Boolean = false;
initDrag(block1);
initDrag(block2);
initDrag(block3);
initDrag(block4);
var TCount:Number = 50;
var TTimer:Timer = new Timer(100,TCount);
TTimer.addEventListener(TimerEvent.TIMER, Tcountdown);
function Tcountdown(e:TimerEvent):void
{
if (TCount == 1)
{
trace("done");
TTimer.removeEventListener(TimerEvent.TIMER, Tcountdown);
gotoAndStop("lose");
}
else
{
Queen.alpha += 0.02;
TCount--;
trace(TCount);
}
}
function initDrag(obj:MovieClip )
{
obj.addEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.addEventListener(MouseEvent.MOUSE_UP,stopADrag);
}
function endDrag(obj:MovieClip )
{
obj.removeEventListener(MouseEvent.MOUSE_DOWN,startADrag);
stage.removeEventListener(MouseEvent.MOUSE_UP,stopADrag);
removeEventListener(Event.ENTER_FRAME, checkForHit);
}
function startADrag(e:MouseEvent):void
{
currentObject = (MovieClip)(e.target);
var rect:Rectangle = new Rectangle(0,0,stage.stageWidth - currentObject.width,stage.stageHeight - currentObject.height + 100);
currentObject.startDrag(false,rect);
dragging = true;
}
function stopADrag(e:MouseEvent):void
{
if (currentObject != null)
{
dragging = false;
currentObject.stopDrag();
}
}
addEventListener(Event.ENTER_FRAME, checkForHit);
function checkForHit(e:Event):void
{
if (dragging)
{
if (block2.hitTestObject(dragtest))
{
endDrag(block2);
removeEventListener(Event.ENTER_FRAME, checkForHit);
TTimer.start();
}
if (block3.hitTestObject(dragtest))
{
removeEventListener(Event.ENTER_FRAME, checkForHit);
endDrag(block3);
removeEventListener(Event.ENTER_FRAME, checkForHit);
gotoAndStop("lose");
}
if (block4.hitTestObject(dragtest))
{
endDrag(block4);
removeEventListener(Event.ENTER_FRAME, checkForHit);
gotoAndStop("lose");
}
if (block1.hitTestObject(dragtest))
{
removeEventListener(Event.ENTER_FRAME, checkForHit);
endDrag(block1);
gotoAndStop("lose");
}
}
}