Skip to main content
Known Participant
June 23, 2012
Answered

Why will this code only trace strings?

  • June 23, 2012
  • 1 reply
  • 683 views

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 = 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?

This topic has been closed for replies.
Correct answer kglad

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.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.

1 reply

kglad
Community Expert
Community Expert
June 23, 2012

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

withertonAuthor
Known Participant
June 23, 2012

Thanks for the reply, but I'm not sure what you mean by that.

I have tried tracing one element at a time with the ".name" appendix and that returns an element of the array without it being in strings.And as I said, if I just put quotation marks around the elements in the "doors" array, then the array works fine.

I'm just a little confused by which array you're talking about and how I should trace it?

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
June 23, 2012

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.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.