PDA

View Full Version : Dynamically referencing objects



chonchonchon
July 12th, 2009, 07:27 PM
Hi
I was wondering if anyone could help me with this simple problem:

The problem is referencing object names dynamically in AS3, eg in a loop

Example
I have an array called c4 which i would like to reference like this

I have a number
var x:Number = 4

How can I do something like

trace(["c"+x].length);
(i know this is wrong!)

Same with other objects
eg. Movieclip called mc4

["mc"+x].x = 100;


Thanks a lot for any help

lemoncurds
July 12th, 2009, 07:44 PM
try

trace(this["c"+x].length);

it should work

chonchonchon
July 12th, 2009, 09:21 PM
Thanks a lot, that worked.
Appreciate the help!

alphaomega
July 13th, 2009, 03:53 AM
Hi lemoncurds

I am somewhat new to AS3.0...

I was hoping you could help me understand what the below code means.
trace(this["c"+x].length);For example,
I wrote this code to try and produce the same effect..


var c1:Array = [1,2,3,4,5];

var c2:Array = [1,2,2,1,1,1,1,1,1];

var c3:Array = [11,2,3,6,22233,4451,223,1];

var c4:Array = [11,2.23,1,2,4,565,1,1,1,1,1,1,1,1,1,1,1,1,];

traceFunc();

function traceFunc()
{
for(var i:Number =1; i<5; i++)
{
var refArray:String ='c'+i;
//trace(refArray); /*outputs c1, c2, c3, c4*/
trace(refArray.length); //output comes out as 2
}
}SOrry if this is highly noObish :frankenstein:

Thanks in advance for any help =)