Search code examples
actionscript-3flash-cs6

Removing instance after several condition


How do i solve the Parameter child must be non-null error?
I'm trying to remove instances (box1, box2, box3, box4) that have .y <= 56

The code follows:

for (i=1; i<=4; i++)
{
    this["Box" + i].addEventListener(Event.ENTER_FRAME, Move);
}

function Move(e:Event):void
{
    e.target.y -=  6;
    if (e.target.y <= 56)
    {
        removeChild(getChildByName(e.target.name));
        Comment.text = "MISS";
        miss +=  1;
        score +=  0;
    }
}

Solution

  • Seems like listener continue to fire after the box is removed, so before this line:

    removeChild(getChildByName(e.target.name));
    

    add this line:

    MovieClip(e.target).removeEventListener(Event.ENTER_FRAME, Move);