Remove Child and AS3 2025 Error

后端 未结 2 979
无人共我
无人共我 2020-12-22 06:37

Trying to remove a bullet from an array and cannot seem to get rid of this error.

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


        
2条回答
  •  春和景丽
    2020-12-22 07:10

    Take a look on the definition of removeChild() :

    Removes the specified child DisplayObject instance from the child list of the DisplayObjectContainer instance.

    So to get that function executed successfully, it should be executed on the DisplayObjectContainer parent of the child ( DisplayObject ) which we will remove, but if we are outside this parent, the compiler will take the current DisplayObjectContainer as the parent, take this example to understand more :

    trace(this);    // gives : [object MainTimeline]
    
    var parent_1:MovieClip = new MovieClip()
        addChild(parent_1);             
        // add the parent_1 to the MainTimeline
    
    var child_1:MovieClip = new MovieClip()
        addChild(child_1);              
        // add the child_1 to the MainTimeline
    
    var child_2:MovieClip = new MovieClip()
        parent_1.addChild(child_2);     
        // add the child_2 to parent_1 which is a child of the MainTimeline         
    
    removeChild(child_1);               
    // remove child_1 from the MainTimeline : OK
    
    try {
        removeChild(child_2);           
        // remove child_2 from the MainTimeline : ERROR
    } catch(e:*){
        trace(e);                       
        // gives : ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    }
    

    Here we know that child_2 is not a child of the MainTimeline and that's why we got the Error #2025 because the supplied DisplayObject ( child_2 ) is not a child of the caller ( the MainTimeline ).

    So to remove the child_2 MovieClip, we have to call removeChild() from it's parent which is parent_1 :

    parent_1.removeChild(child_2); // OK
    

    And to simplify or if we don't know it's parent, you can write it :

    child_2.parent.removeChild(child_2);
    

    Hope that can help.

提交回复
热议问题