0
votes

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");

        }

    }
}
3
Ok, I figured it out I just had to create a workaround so that a variable is true for each thing that hits. Then have something test the variables for true. Reaaaaaly stupid error will post code later. - Flashdace

3 Answers

1
votes

you can check, if TTimer object exist and then remove event listener

if(TTimer)
{
    TTimer.removeEventListener(TimerEvent.TIMER, Tcountdown);
}
1
votes

Queen, block1, block2, block3 and block4 are null or otherwise undefined. No amount of fiddling with the timer will fix this. Check your instance names, ensure the objects you're referring to are on the same frame as the code being executed.

0
votes

Perhaps you should simplify your checkForHit function

function checkForHit(e:Event):void
{
    if (dragging)
    {
        if (block2.hitTestObject(dragtest)) 
        {
            endDrag(block2);
            TTimer.start();
        }
        else 
        {
            if (block1.hitTestObject(dragtest))
            {
                endDrag(block1);
            }
            if (block3.hitTestObject(dragtest))
            {
                endDrag(block3);
            }
            if (block4.hitTestObject(dragtest))
            {
                endDrag(block4);
            }
            gotoAndStop("lose");
        }
        // removeEventListener here is not needed because it is within endDrag()
    }
}