Actionscript – 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){

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?

Best Solution

Iterate through the length, not the items:

for (var i=0; i < inventory.length; 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("Mana Potion");