Search code examples
actionscript-3flash

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


I'm creating a small game in as3.0. I spawn a lot of walls in the game that are trying to crush you (the player).

I'm trying to delete all the walls that are near you from the array and from the screen. The first time I hit the button it works it just deletes all the walls that are in a 250px range. But the second time I hit the button get the following error:

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

I think this is because the tries to delete childs who already has been deleted. I tried to check if the child existed with the following code but it doesn't seem to work.

if (wallArray[i] != null && contains(wall)) {
    if (wallArray[i].x < 250 + wp_reach){   
        //haalt de muur weg
        //TODO : KIJK OF HET KIND IS

            removeChild(wallArray[i]); 
         }

    }

Here is the full code that handels the button press:

Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

action.addEventListener(TouchEvent.TOUCH_TAP, fl_TapHandler);

function fl_TapHandler(event:TouchEvent):void
{
    for (var i:Number=0;i<wallArray.length;i++){
        if (wallArray[i] != null && contains(wall)) {
            if (wallArray[i].x < 250 + wp_reach){   
                removeChild(wallArray[i]); 
            }

        }
        else{
            trace ("There is no wall in range yet");
        }
    }   
}

Solution

  • Always test if item in query is the child of the container you want to remove it from as follows:

    if(wallArray[i] && contains(wallArray[i]))
    {
        removeChild(wallArray[i]);
    }
    

    best regards