Tareq Hassan
Tareq Hassan

Reputation: 5

as3 using addChild with an array containing indexes

so i have an array containing many instances. let's say movieclips.

and i have another array which contains numbers..in this case those numbers represent selected indices that i've somehow chosen!

var manydots:Array = new Array ();

for (var i=0; i<10; i++)
{
    var newDot:dot = new dot  ;
    manydots.push(newDot);
}

var indices:Array = [0,1,5,8,4]

i want to use AddChild to add those movieclips into my scene, but not all of them, only selected indices contained in my 2nd array

Upvotes: 0

Views: 2446

Answers (2)

sch
sch

Reputation: 27536

sberry solution is correct. But you may also want to check that you actually are not adding null as a child.

for each(var i:int in indices) {
    if (i < manydots.length) {
        var d:dot = manydots[i];
        if (d) {
            addChild(d);
        }
    }
}

Upvotes: 1

sberry
sberry

Reputation: 132138

I think this is what you are looking for,

for (var j=0; j<indicies.length; j++) {
    addChild(manyDots[incidies[j]]);
}

Upvotes: 2

Related Questions