Take the 2-minute tour ×

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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");
share|improve this answer
Thank you, i was just using = to test out the array, but I'll keep push in mind. Any idea about the second problem? –  ThatBenderGuy Apr 30 '12 at 0:58
@ThatBenderGuy For your second issue you want to be doing this: this["item" + i].text = inventory[i]; –  Marty Apr 30 '12 at 1:02
Thank you so very much! –  ThatBenderGuy Apr 30 '12 at 1:39
add comment

Your Answer


