Can't add scrollpane properly from class

Hello, I created many movieclips and added them into my scrollpane source. The code is working fine when I test it with .fla file. However I’m new to classes and it’s giving me error: RangeError: Error #2006: The supplied index is out of bounds.

Here’s my class code:

public function init():void
{
    ...

    /*create DispalyObject to act as the source - the actual content that appear in th scroll pane, this can also be a direct url..." */
    mySP.source = spSource;

    /*add the sp to the stage*/
    addChild(mySP);
}

public function addBoxes(isUpdate:Boolean):void
{

    ...

    mySP.update();
    // return(spSource);
}

And my fla:

function firstEdit():void {
    boxAdder.init();
    boxAdder.addBoxes(false);
    addChild(boxAdder.spSource);

    // If I use the code below, it's giving me error
    // Otherwise scrollpane is invisible and the spsource is shown without errors
    boxAdder.spSource.addChild(boxAdder.mySP);
    boxAdder.mySP.update();

    isEditSet = true;
}

How can I add the scrollpane to stage?

I think you have an excess of addChilds happening.

I take it your boxAdder is a display object? Do you want your scroll pane (which is mySP?) in boxAdder?

You seem to be adding mySP in boxAdder.init(), which is fine. But you’re also calling boxAdder.spSource.addChild(boxAdder.mySP); which actually doesn’t make a lot of sense since you’re now trying to add the scroll pane to the scroll pane source, when its the scroll pane source is what ultimately gets added to the scroll pane. Now you’re just asking for the universe to explode!

You really only need two addChilds here. The first one that you can keep is addChild(mySP) in what I’m assuming is the class to boxAdder. So boxAdder now contains your scroll pane which I’m assuming is mySP. In there, you’re also setting source which is good. This implicitly calls addChild to add the source into the scroll pane. You don’t have to do this yourself; its automatic.

The other addChild is getting your boxAdder on the stage. You may have already done this, I don’t know. I don’t see where boxAdder is created. It may even be on the timeline already, in which case no more addChilds are needed, but if its not, then you need an addChild(boxAdder) in there too.

addChild(boxAdder.spSource); is not needed because spSource is set as the source of mySP so it is automatically added to mySP

boxAdder.spSource.addChild(boxAdder.mySP); is not needed because mySP is already added to boxAdder in boxAdder.init, and if you wanted to add something into boxAdder.spSource, it can’t very well be the scroll pane it was added to. :slight_smile:

Hello, I read your text carefully, boxAdder is my class file. The sp source contains 2d arrays of movieclips.

This solved my problem. Here’s my final code:

function firstEdit():void {
    addChild(boxAdder);
    boxAdder.init();
    boxAdder.addBoxes(false);
    
    boxAdder.mySP.update();
    
    isEditSet = true;
}

Thank you for your reply.