Skip navigation
Currently Being Moderated

Why will this code only trace strings?

Jun 23, 2012 7:58 AM

The following code randomizes the array "doors". It works fine if the array is strings. However,

if I remove the quotation marks, it traces "[object Symbol1_1]" instead.

 

var doors:Array = [red_button, blue_button,yellow_button];

var randomDoors:Array = new Array(doors.length);

 

var randomPos:Number = 0;

for (var i:int = 0; i < randomDoors.length; i++)

{

    randomPos = int(Math.random() * doors.length);

    randomDoors[i] = doors.splice(randomPos, 1)[0]; 

}

trace (randomDoors);

 

Tracing (randomDoors.name) returns the  statement "undefined". How can I trace "doors" without converting the elements to strings?

 
Replies
  • kglad
    72,227 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 23, 2012 8:44 AM   in reply to witherton

    assuming those doors elements have names, loop through that array and trace each elements name.

     
    |
    Mark as:
  • kglad
    72,227 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 23, 2012 9:02 AM   in reply to witherton

    the array class doesn't have a name property.  when you use trace(randomDoors.name), you're asking flash to find the name property of that array.

     

    so, either use:

     

    for (var i:int = 0; i < randomDoors.length; i++)

    {

    trace(doors[i].name);

    }

     

    or create your own function or class to do what you want so you don't have to repeatedly create a for-loop to trace what you want.

     
    |
    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