1
votes

I am making a photo gallery simple enough, but when i try to categorize the photos i keep getting this error when i hit the button. the catagory button removes the thumbs but does not catagorise them.

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display::DisplayObjectContainer/removeChild()
    at myGallery_fla::MainTimeline/removeAllMovieClips()
    at myGallery_fla::MainTimeline/loadConcert()

I am pulling my hair out trying to fix it and i just cant get it. Any help would be really appreciated :)

heres my code its quite long :

//declaring my permanant variables.
var columns:Number;
var my_x:Number;
var my_y:Number;
var my_thumb_width:Number;
var my_thumb_height:Number;
var my_images:XMLList;
var my_total:Number;

var currentCategory:String = "concert";

//new movie clip to hold images.
var container_mc:MovieClip;
//new movie clip to do progress bar.
var preloaders_mc:MovieClip;
//to give full image a hand cursor. button mode does not work on loaders so i must create a new mc.
var full_mc:MovieClip;

var x_counter:Number = 0;
var y_counter:Number = 0;

//array to hold tweens.
var my_tweens:Array = [];
//array for categorizing
var movieClipArray:Array=new Array();

//permenant vars for tweens.
var container_mc_tween:Tween;
var full_tween:Tween;

var myNewXML:XMLList;
var myXML:XML;
//creater new urlLoader to load xml
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(new URLRequest("gallery.xml"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);


function processXML(e:Event):void{
    //temperary will be deleted when executed.
     myXML= new XML(e.target.data);
    processXMLData();
}

function processXMLData()
{
myNewXML = myXML.GALLERY.(CATEGORY == currentCategory);
//trace(myNewXML);
    //set the values of the images 
    //@ retrives the attributes I need
    columns = myXML.@COLUMNS;
    my_x = myXML.@XPOSITION;
    my_y = myXML.@YPOSITION;
    my_thumb_width = myXML.@WIDTH;
    my_thumb_height = myXML.@HEIGHT;
    my_images = myXML.IMAGE;
    my_total = my_images.length();



    createContainer();
    callThumbs();

    //instance of the URLLoader and its associated event listener are 
    //both not required once we extract the data from our XML file.
    //delete these two once that task is finished.
    myXMLLoader.removeEventListener(Event.COMPLETE, processXML);
    myXMLLoader = null;

}


function createContainer():void{
    //creating movieclip to hold images
    container_mc = new MovieClip();



    //I position the MovieClip in accordance to the variables from xml.
    container_mc.x = my_x;
    container_mc.y = my_y;
    addChild(container_mc);

    //regestering the click event to the parent instead of the thumbs
    //instead of to each thumb.
    container_mc.addEventListener(MouseEvent.CLICK, callFull);
    //thumbs hover effects
    container_mc.addEventListener(MouseEvent.MOUSE_OVER, onOver);
    container_mc.addEventListener(MouseEvent.MOUSE_OUT, onOut);
    container_mc.buttonMode = true;

    preloaders_mc = new MovieClip();
    preloaders_mc.x = container_mc.x;
    preloaders_mc.y = container_mc.y;

    //movieClipArray.push(container_mc);
    //trace(movieClipArray);
    addChild(preloaders_mc);
    //trace(movieClipArray);
}

function callThumbs():void{
    //loop will cycle through as long as it does not exceed the total num of images.
    for (var i:Number = 0; i < my_total; i++){

        //retrives the url of the thumbs.
        var thumb_url = my_images[i].@THUMB;
        //creates temperary instance of loader
        var thumb_loader = new Loader();
        //loads the thumbs
        thumb_loader.load(new URLRequest(thumb_url));
        thumb_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbLoaded);

        //unique reference to each thumb to tell us what image to load from the xml array.
        //thus I set thumb's name property as its number in the XML list.
        thumb_loader.name = i;

        //position the thumbs horizontially
        thumb_loader.x = (my_thumb_width + 30)* x_counter;

        thumb_loader.y = (my_thumb_height + 30)* y_counter;


        if(x_counter+1 < columns){
            x_counter++;
        }
        else{
            x_counter = 0;
            y_counter ++;
        }

        var preloader_pb:ProgressBar = new ProgressBar();
        preloader_pb.source = thumb_loader.contentLoaderInfo;
        preloader_pb.x = thumb_loader.x;
        preloader_pb.y = thumb_loader.y;
        preloader_pb.width = my_thumb_width;
        preloader_pb.height = my_thumb_height;
        preloaders_mc.addChild(preloader_pb);

        preloader_pb.addEventListener(Event.COMPLETE, donePb);
    }
}


//loads the instances of thumbs to container_mc to be visable.
function thumbLoaded(e:Event):void{
    var my_thumb:Loader = Loader(e.target.loader);
    container_mc.addChild(my_thumb);
    //trace(container_mc);
    stage.addChild(container_mc);
    movieClipArray.push(container_mc);
    //trace(movieClipArray.length);

    //Fading in Each Thumb once it Loads.
    my_tweens[Number(my_thumb.name)]=new Tween(my_thumb, "alpha", Strong.easeIn, 0,1,0.5, true);

    //event listener used to check the download process of our thumbnails. 
    //These are only used once in the gallery and are never required again.
    my_thumb.contentLoaderInfo.removeEventListener(Event.COMPLETE, thumbLoaded);

}

//loads the full image by using an instance of loader class.
function callFull(e:MouseEvent):void{
    var full_loader:Loader = new Loader();
    var full_url = my_images[e.target.name].@FULL;
    full_loader.load(new URLRequest(full_url));
    //will only be displayed after it fully loaded
    full_loader.contentLoaderInfo.addEventListener(Event.INIT, fullLoaded);

    var full_pb:ProgressBar = new ProgressBar();
    full_pb.source = full_loader.contentLoaderInfo;
    full_pb.x = 100;
    full_pb.y = 400;
    preloaders_mc.addChild(full_pb);

    full_pb.addEventListener(Event.COMPLETE, donePb);


    container_mc.removeEventListener(MouseEvent.CLICK, callFull);
    container_mc.buttonMode = false;
    //hover effects for full.
    container_mc.removeEventListener(MouseEvent.MOUSE_OVER, onOver);
    container_mc.removeEventListener(MouseEvent.MOUSE_OUT, onOut);
    //Fading In and Out the Entire Gallery when a full image is shown
    container_mc_tween = new Tween(container_mc, "alpha", Strong.easeIn, 1,0.5,0.5, true);

}

//will load the full pic and position it
function fullLoaded(e:Event):void{
    //for the hand cursor.
    full_mc = new MovieClip();
    full_mc.buttonMode = true;
    addChild (full_mc);
    var my_loader:Loader = Loader(e.target.loader);
    full_mc.addChild(my_loader); // This line was addChild(my_loade), just add full_mc. before it.
    //Fading In of Full Image
    full_tween = new Tween(my_loader, "alpha", Strong.easeIn, 0,1,0.5, true);
    my_loader.x = 150;
    my_loader.y = 100
    my_loader.addEventListener(MouseEvent.CLICK,removeFull);

    my_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, fullLoaded);
    //stage.addEventListener(MouseEvent.ROLL_OUT, disappear,false,0,true);
    full_mc.addEventListener(MouseEvent.ROLL_OVER,appear);

    }




function removeFull(e:MouseEvent):void{
var my_loader:Loader = Loader (e.currentTarget);
full_tween = new Tween(my_loader, "alpha", Strong.easeOut, 1,0,0.5, true);
full_tween.addEventListener(TweenEvent.MOTION_FINISH, tweenFinished);

container_mc_tween = new Tween(container_mc, "alpha", Strong.easeOut, 0.5,1,0.5, true);
my_loader.addEventListener(MouseEvent.ROLL_OUT,disappear);
}

function tweenFinished (e:TweenEvent):void{
var my_loader:Loader = Loader (e.target.obj);
my_loader.unload();
full_mc.removeChild(my_loader); // This line was removeChid(my_loader), just add full_mc before it.
removeChild(full_mc);
full_mc = null;

container_mc.addEventListener(MouseEvent.CLICK, callFull);
container_mc.buttonMode = true;
container_mc.addEventListener(MouseEvent.MOUSE_OVER, onOver);
container_mc.addEventListener(MouseEvent.MOUSE_OUT, onOut);

var my_tween:Tween = Tween(e.target);
my_tween.removeEventListener(TweenEvent.MOTION_FINISH, tweenFinished);
}

function donePb (e:Event):void{
    var my_pb:ProgressBar = ProgressBar(e.target);
    preloaders_mc.removeChild(my_pb);
    my_pb.removeEventListener(Event.COMPLETE, donePb);
}

function onOver (e:MouseEvent):void{
    var my_thumb:Loader = Loader(e.target);
    my_thumb.alpha = 0.5;
}

function onOut (e:MouseEvent):void{
    var my_thumb:Loader = Loader (e.target);
    my_thumb.alpha = 1;
}

//category buttons
var animal_btn:SimpleButton = new animals_mc();
addChild(animal_btn);
animal_btn.x = 50;
animal_btn.y =550;

var concert_btn:SimpleButton = new concert_mc();
addChild(concert_btn);
concert_btn.x = 180;
concert_btn.y =550;

var festival_btn:SimpleButton = new festival_mc();
addChild(festival_btn);
festival_btn.x = 310;
festival_btn.y =550;

var flowers_btn:SimpleButton = new flowers_mc();
addChild(flowers_btn);
flowers_btn.x = 440;
flowers_btn.y =550;


flowers_btn.addEventListener(MouseEvent.CLICK, loadFlowers);
function loadFlowers(event:MouseEvent)
{
    currentCategory = "flower";
    removeAllMovieClips();
    processXMLData();
}

animal_btn.addEventListener(MouseEvent.CLICK, loadAnimals);
function loadAnimals(event:MouseEvent)
{
    currentCategory = "animals";
    removeAllMovieClips();
    processXMLData();
}

concert_btn.addEventListener(MouseEvent.CLICK, loadConcert);
function loadConcert(event:MouseEvent)
{
    currentCategory = "concert";
    removeAllMovieClips();
    processXMLData();
}

festival_btn.addEventListener(MouseEvent.CLICK, loadFestival);
function loadFestival(event:MouseEvent)
{
    currentCategory = "festival";
    removeAllMovieClips();
    processXMLData();
}

function removeAllMovieClips(){
    trace(movieClipArray.length);
    for (var i =0; i<movieClipArray.length; i++){
        stage.removeChild(movieClipArray[i]);
    }
    movieClipArray = [];
    //trace(myMovieClipsOnStage);

}

var menu_mc:MovieClip = new fullPicMenu_mc();
addChild(menu_mc);
menu_mc.x = 150;
menu_mc.y =460;
menu_mc.visible= false;




var share_btn:SimpleButton = new share_mc();
addChild(share_btn);
share_btn.x = 80;
share_btn.y =350;
share_btn.visible= false;

var fav_btn:SimpleButton = new fav_mc();
addChild(fav_btn);
fav_btn.x = 93;
fav_btn.y =400;
fav_btn.visible= false;

var caption_btn:SimpleButton = new caption_mc();
addChild(caption_btn);
caption_btn.x = 80;
caption_btn.y =460;
caption_btn.visible= false;
2

2 Answers

0
votes

You are pushing the same container_mc more than once into movieClipArray, then you are attempting to remove it from the stage twice. Either make a new container at thumbLoaded() function each time a thumb is loaded, or get rid of the array completely, since you will have only one container.

0
votes
function removeAllMovieClips(){
    trace(movieClipArray.length);
    for (var i =0; i<movieClipArray.length; i++){
        if(movieClipArray[i] is DisplayObject && movieClipArray[i].parent == stage)
            stage.removeChild(movieClipArray[i]);
    }
    movieClipArray = [];
}

I didn't go through all your code to find out why your movieclip isn't a child of stage, but this will prevent the error.

you could also do

if(movieClipArray[i].parent) movieClipArray[i].parent.removeChild(movieClipArray[i]);