Can't removeChild with this button

I probably need to back out of this whole method that I’m trying to use for this, but I need to know where it’s going wrong. I’m missing a fundamental here…

I’m trying to make a simple photo gallery that steps through photos in my library. The photos are in the library because I need to produce a self contained SWF that I can use in a PDF via InDesign.

On the stage I have an empty “loader” movieclip that’s on a layer below a black frame that I want the “photo movieclips” in my library to be loaded underneath. I can load the initial photo with no problem:

for (var i:uint = 0; i<imageObjArray.length; i++)
    {
        if (index == imageObjArray*.id)
        {
            var ClassReference:Class = getDefinitionByName(imageObjArray*.imageFile) as Class;
            var newImage = new ClassReference();
            var title:String = imageObjArray*.title;
            break;
        }
    }
    poiPanel_mc.title_txt.htmlText = "<b>"+title+"</b>";
    currentChild = poiPanel_mc.loader_mc.addChild(newImage);

I’m using getDefinitionByName because I have all of the names of the Classes for the photo movieclips in my library stored as Strings in an array. This is the first thing that seems silly about the way I’m doing this. I have to make movieclips with Classes for each photo…really?

Next you’ll see that I am using addChild to load the first photo to the “loader” movieclip under the frame. This works.

Now, also on the stage, and within a movieclip, I have “next” and “previous” buttons. It is with these buttons I would like browse through the images.

My assumption is that I need to “removeChild” each time a new photo movieclip from the library is added, otherwise things would just keep piling on top and take up memory.

So, for my button code, I’ve started with the following on the “next” button:


poiPanel_mc.nextBtn_mc.buttonMode = true;
    poiPanel_mc.nextBtn_mc.addEventListener(MouseEvent.MOUSE_DOWN ,nextOnDown);
    function nextOnDown():void {
    poiPanel_mc.nextBtn_mc.gotoAndPlay(2);
    removeChild(currentChild);
    }

You’ll see that I’m trying to removeChild passing the variable I populated when I used addChild. However, when I run the movie and click the button I get the “…must be a child of the caller.” Error.

Does this mean I need to somehow load the images onto the button movie clip??

What I’d really like is for someone to say “dude, you’re totally lost, here’s the correct 5 lines of code way to do this…”

But I’d also like to know the technical reasons why this method is failing.

What I should probably do is add all of the photos to separate frames on a movieclip on the stage and just use the buttons to step through the frames… But I seek to understand this add/removeChild business.

ThaNks for any help on this. :beer2: