Skip navigation
NickelBuckle9
Currently Being Moderated

Simple for loop with array?

Jul 18, 2012 2:18 PM

Tags: #as3 #visible #array #string #object

Just a quick question!  I've been staring at this for the past 2 hours and I have looked everywhere, but still no success.

 

 

I have a simple array containing items.  When the user changes the page, I want these items in the array to become visible.

 

 

var outfit:Array = ["Doll.Drawers.Shirt1", "Doll.Drawers.Pants1", "Doll.Drawers.Jacket1"];

function f_item_show(event:MouseEvent):void
          {
                    for(var i:int = 0; i < outfit.length; i++)
                    { 
                              trace(outfit[i]);
                              this[outfit[i]].visible = true;  // THIS IS THE ISSUE //
  
                    }
          }

 

The items in the array are strings of the instance names, so i just need each string to be converted to an object and set the .visible part of each item to true.

 

I know my loop is working right, becuase the trace accurately reads each instance name in the list.  I figured the this[] part would solve my problem, but it doesn't... I keep getting this error:

 

 

TypeError: Error #1010: A term is undefined and has no properties.
          at PokeDress_fla::MainTimeline/f_item_show()

 

I know it's that one line of code, because it works perfectly when I remove it.  All my instance names are correctly labeled and added, because they're used many other times in the code flawlessly. 

 

What am I doing wrong?

 
Replies
  • Currently Being Moderated
    Jul 18, 2012 2:57 PM   in reply to NickelBuckle9

    To use the bracket notation, each bracket can only repesent one object, so you would need three brackets since there are three objects in your string... meaning you would need three arrays.

     

    If they all involve target something inside the Doll.Drawers object, then you could just store the shirts and pants and whatnot in the array as strings and use...

     

    Doll.Drawers[outfit[i]].visible = true;

     

     

    What you should try instead is to store the actual paths to the objects instead of string representations of them, as in...

     

    var outfit:Array = [Doll.Drawers.Shirt1, Doll.Drawers.Pants1, etc...

     

    then you should be able to use...

     

    outfit[i].visible = true;

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 18, 2012 4:35 PM   in reply to NickelBuckle9

    You're welcome

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points