Search code examples
flashruntime-errorremovechild

Error #2025: The supplied DisplayObject must be a child of the caller


I just started making a really simple video game for a class and I have a random score point spawner. This function works fine, nothing goes wrong, but when I publish the game and play it with Flashy Player, it shows up with the error message

Error #2025: The supplied DisplayObject must be a child of the caller.

I've just been dismissing it since the program works other than the alert prompt, but I need to remove that.

function spawnscore()
{
    i = 0
    while (i == 0)
    {
        var pointy = Math.random()*640
        var pointx = Math.random()*747

        var pointcirc:warning = new warning();
        addChild(pointcirc);
        pointappearmusic.play();
        setTimeout(removepoint, 1500);
        pointcirc.addEventListener(MouseEvent.MOUSE_OVER, scoreclicked);

        function scoreclicked()
        {
            pointsound10.play();
            removeChild(pointcirc);
            score += 10;
            removeEventListener(MouseEvent.MOUSE_OVER, scoreclicked);
        }

        function removepoint()
        {
            // I'm pretty sure this is the problem
            removeChild(pointcirc);
        }

        pointcirc.x = pointx;
        pointcirc.y = pointy;
        break;
    }

    return;
}

I'm pretty sure my problem is in the removepoint function, but I can't figure out what to do.

Edit : highlight error


Solution

  • That error means you're trying to remove a DisplayObject from a container when it's not actually a child of that container, so it looks like what's happening is scoreclicked() runs and removes pointcirc, then removepoint tries to remove it again.

    You just need to check if pointcirc has already been removed in removepoint(). There are lots different ways to do that: You could set a pointRemoved variable, or just set pointcirc = null after it's removed and check that. You can also check directly using DisplayObjectContainer.contains(), or just see if pointcirc.parent exists.