Be a Supporter!

length property not working

  • 310 Views
  • 2 Replies
New Topic Respond to this Topic
kylelyk
kylelyk
  • Member since: May. 15, 2008
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
length property not working 2009-05-02 17:48:02 Reply

So I have this array and the length property of it doesn't seem to be working when i make it have one number, the length is that number:

var primary:Array = new Array(5);
trace(primary.length)//outputs 5

but when i have two numbers in it, the length property works:

var primary:Array = new Array(5,0);
trace(primary.length)//outputs 2

What am I doing wrong?


Kylelyk: Forwards and Backwards, Over and Out.

BBS Signature
Blue-Devil23
Blue-Devil23
  • Member since: Sep. 4, 2005
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to length property not working 2009-05-02 18:01:51 Reply

Don't worry. That's supposed to happen.

When you make an array with one value, it tells flash that the value is the number of elements in said array. If you make one with two or more, those values become the elements in that array.

If you want an array with a single value put:

var primary:Array = new Array();
primary[0] = 5;
trace(primary.length);

This outputs 1.

For more info read this

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to length property not working 2009-05-02 18:07:33 Reply

To make an array I suggest using the square bracket operators. You can do this:

var list:Array = [5];

That will make the array with just one value, the number 5. Apparently it's also slightly faster to create an array with that than using the Array() function like you did; probably because it doesn't check for amount of arguments and whether to create the list with the arguments as elements or as the length :p


Sup, bitches :)

BBS Signature