Search code examples
flashactionscript-3containsremovechildgsap

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


I'm trying to create a slideshow style presentation, where each slide/page is a movieclip and they transition via a sliding animation. The problem I have is that I don't want all the movieclips on stage at once to prevent lag issues, an example of what I'm trying to achieve:

  • Current page is Page A, next button is clicked
  • Page B is placed on the stage using addChild (but placed out of view)
  • Page A slides off the visible stage
  • Page B slides into the visible stage
  • Page A is removed from stage using removeChild - this is where I am having problems.
//greensock stuff
import com.greensock.*;
import com.greensock.easing.*;

//buttons that navigate to next and previous slides
prevBtn.addEventListener(MouseEvent.CLICK,prevClicked,false,0,true);
nextBtn.addEventListener(MouseEvent.CLICK,nextClicked,false,0,true);

//setting up array of all the pages
var pageArray:Array = [page1, page2, page3, page4, page5];
var currentArray:Number = 0;

//defining the first page and placing it on the stage
var currentPage:MovieClip = new pageArray[currentArray];
addChild(currentPage);



function nextClicked(event:MouseEvent):void{
    //check to ensure it is not the last page
    if (currentArray < pageArray.length - 1){
            //define the current slide as oldPage
            var oldPage:MovieClip = new pageArray[currentArray];
            currentArray++;
            //define the next slide as currentPage and place on stage
            var currentPage:MovieClip = new pageArray[currentArray];
            addChild(currentPage);
            currentPage.x = 1024;
            TweenMax.to(currentPage, 1, {x:0});
            TweenMax.to(oldPage, 1, {x:-1024,onComplete:removeChild,onCompleteParams:[oldPage]});
    }
}



function prevClicked(event:MouseEvent):void{
    //check to ensure it is not the first page
    if (currentArray > 0){
            //define the current slide as oldPage
            var oldPage:MovieClip = new pageArray[currentArray];
            currentArray--;
            //define the next slide as currentPage and place on stage
            var currentPage:MovieClip = new pageArray[currentArray];
            addChild(currentPage);
            currentPage.x = -1024;
            TweenMax.to(currentPage, 1, {x:0});
            TweenMax.to(oldPage, 1, {x:1024,onComplete:removeChild,onCompleteParams:[oldPage]});    }
}

Solution

  • It is likely your "oldPage" is not yet added to the stage/movieClip at some point, like right at the beggining.

    So instead of:

    TweenMax.to(oldPage, 1, {x:-1024,onComplete:removeChild,onCompleteParams:[oldPage]});
    

    Consider to first check if the child still exists before removing it. This would require calling a new function, something like:

    (untested code)

    TweenMax.to(oldPage, 1, {x:-1024,onComplete:tryRemoveChild,onCompleteParams:[oldPage]});
    
    function tryRemoveChild(page:MovieClip):void {
       if(contains(page)){ //Check if child is still there
           removeChild(page); //It is, so remove
       } //Otherwise there is no child to currently remove
    }