flash as3 supplied index is out of bounds

I'm trying to remove children from a movie clip upon user interaction but it's saying "the supplied index is out of bounds" - however, I must be missing something because it doesn't seem like anything is out of bounds. This is my code:

Circle.as:

for (var i=0; i<3;i++){ //this number should be based on the number of children found in the XML
            var wedge:Wedge = new Wedge(wedgeHolderRef, i, cr,cScale);
        }

Wedge.as:

public function Wedge(wedgeHolderRef, wedgeNum:int, cr:int = 3, cScale:int = 5)  
{
    var wedge:Sprite = new Sprite();
    var wedgeClip:MovieClip = new MovieClip();
    wedgeClip.addChild(wedge);
    wedgeHolderRef.addChild(wedgeClip);
}

private function wedgeClickHandler(wedgeRef):void
{
    var wedgeChildren =  wedgeHolderRef.numChildren;
    for (var i=0; i<wedgeChildren; i++)
    {
        var wedgeClip = wedgeHolderRef.getChildAt(i);
        if (i != wedgeChildren-1){

        } else {
            deactivateCircle();
        }
    }
}

private function deactivateCircle()
{
    var wedgeChildren =  wedgeHolderRef.numChildren;
    for (var i=0; i<wedgeChildren; i++){
        //trace (i + "|" + wedgeHolderRef.getChildAt(i).wedgeNum);
        wedgeHolderRef.removeChildAt(i); //Here is where I get the error
    }
}

Answers


While you are removing children, your index does not account for the number of children decreasing.

After one iteration, numChildren drops by 1 but your index remains based upon the original child count.

while (wedgeHolderRef.numChildren > 0)
    wedgeHolderRef.removeChildAt(0);

You should not iterate from 0 to the number of children you stored there because i will go higher than the actual number of children at some point. The number of children decreases every time you're removing one.

Instead do this:

while(wedgeHolderRef.numChildren)
{
    wedgeHolderRef.removeChildAt(0);
} 

Need Your Help

Rewriting a tcp stream on the fly: how difficult is it? How about taking a dump of said stream?

java linux networking stream tunnel

I'm trying to write a tcp stream 'tunnel' (similar to the ones SSH handles by default) but with one exception, I have to rewrite certain information as it flows through.

Trying to build simple android but have a schema issue Xamarin

c# android xml xamarin

I'm trying to run a simple android app on VS 2015 and using Xamarin &amp; C#.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.