PDA

View Full Version : Mixed arrays?



m90
March 8th, 2009, 02:07 PM
Hello!

I always thought this (mixed arrays) was possible, but now as I want to use it it won't work!!

I'm trying to create an array consisiting of 4 Vector3D objects and a Number. Yet Flash won't let me create such an array like:

var myArray = new Array(new Vector3D(1,2,3),new Vector3D(2,3,4),new Vector3D(3,4,5),new Vector3D(4,5,6),new Number(0));
Am I missing something here with the creation of the Array or is it just not possible and will I have to use an "Object" instead. Of course I could also just store my Number in the .x a fifth Vector3D-Object but this doesn't seem very practical to me.

Thank you!

wvxvw
March 8th, 2009, 02:29 PM
It should complain about:
1. You don't specify variable type.
2. new Number(0) is kind of "strange" because normally you don't use constructors with primitive types, and with types like int, uint, Number, Boolean and String it even doesn't make sense...
3. Usually, if you want to initialize array to some certain set of values you use hash access syntax - [], not constructor. The only reason to use Array's constructor is for situations when you want to create an array of certain length, but don't want to populate it immediately.

But, technically, there should be no problem to do what you want.

m90
March 8th, 2009, 02:35 PM
Hello & thank you for your answer.

Unfortunately 1. will not make any difference. Regarding 2. the 0 is just a placeholder in my forum post as the array gets populated immediately via input, so it might be 6837 as well and 3. I thought as I alread have everything ready when creating the array I could do it in one line of code instead of 6. Bad Idea?

I also tried using hash access but it didn't make any difference, as soon as I was trying to insert the number, it complained!

wvxvw
March 8th, 2009, 04:35 PM
Sorry, I probably haven't made it clear enough... Your code works, it's just badly written, but it works...

var myArray:Array = [new Vector3D(1, 2, 3),
new Vector3D(2, 3, 4),
new Vector3D(3, 4, 5),
new Vector3D(4, 5, 6),
0];
trace(myArray);

m90
March 8th, 2009, 04:41 PM
Thanks again, I found the mistake, it was another function that was processing the array that was throwing the error, knowing that I can mix whatever I want.

So what would be the advantage of going [0,1,2,3] instead of new Array(0,1,2,3)? I'm just curious as the documentation told me nothing about the []-method and suggests new Array() instead?

Thanks

wvxvw
March 8th, 2009, 04:47 PM
Actually, documentation suggests the opposite:



Array literals

Use Array literals rather than new Array().

Do this:

[]

Not this:

new Array()

And this:

[ 1, 2, 3 ]

Not this:

new Array(1, 2, 3)

Use the Array constructor only to allocate an array of a prespecified size, as in new Array(3), which means [ undefined, undefined, undefined ], not [ 3 ].
Hash access makes flash initialize array to it's value (i.e. write the array object not invoking it's constructor. As Arrays have very simple structure they may initialized like string / number without calling constructor). So, it is a little bit faster than to call Array's constructor. But, unless you create thousands of arrays in a loop you won't feel the difference.

m90
March 8th, 2009, 04:52 PM
Ok! I'll try to keep that habit then.

Guess the translator were a little sleepy over here then: http://help.adobe.com/de_DE/AS3LCR/Flash_10.0/Array.html

Thanks!

TheCanadian
March 8th, 2009, 05:29 PM
2. new Number(0) is kind of "strange" because normally you don't use constructors with primitive types, and with types like int, uint, Number, Boolean and String it even doesn't make sense...

Just thought I'd say that, in AS2, literals and objects of primitive data types were handled differently. To call a method of the String class on a string literal, for example, Flash would have to convert the literal to an object, call the method on the object and then convert the object back to a literal value.

That doesn't apply to AS3 but figured it might be worth knowing ;)