ThatBenderGuy
ThatBenderGuy

Reputation: 307

Flash AS2 - For each loop returning odd order array

Here is my code

var inventory = new Array();
inventory[0] = "Potion";
inventory[1] = "Mana Potion";
inventory[2] = "Key";

for(s in inventory){
    trace(s);
}  

But the trace returns 2, 1, 0. So then I try adding inventory.reverse(); but with that I get 2, 0, 1. Why not 0, 1, 2? How can I fix this?

On another note, how can I relay these variables to textboxes based on the index?
I tried

for(s in inventory){
    "item" + s = inventory[s];
}

The text boxes on the stage have the variables of (item1, item2, item3, etc...)

but that doesn't work, any tips?

Upvotes: 0

Views: 6166

Answers (1)

lunixbochs
lunixbochs

Reputation: 22415

Iterate through the length, not the items:

for (var i=0; i < inventory.length; i++) {
    trace(i);
}

It's also less specific to add items to an array using push(), so you don't need to know beforehand how many items are already in it.

inventory.push("Potion");
inventory.push("Mana Potion");
inventory.push("Key");

Upvotes: 1

Related Questions