0

I'm trying to add several sprites and their associated label to a create js container object using a for loop.

Is it possible to add both at the same time, like so?

NPCs.push(spriteBMP, spriteBMP2, spriteBMP3);
Lables.push(spriteLabel, spriteLabel2, spriteLabel3);

Loop:

for (npc in NPCs && label in Labels) {
    spriteContainer.addChild(npc, label);
}

If not, how can I achieve that?

user3871
  • 12,432
  • 33
  • 128
  • 268

1 Answers1

1
// Using Math.max to make sure we can get through all  of them
for ( var i = 0, l = Math.max(NPCs.length, Labels.length); i < l; ++i ) {
    spriteContainer.addChild(NPCs[i], Labels[i]);
}

Anything that is not in one of the arrays will be sent as undefined.

kalley
  • 18,072
  • 2
  • 39
  • 36