PDA

View Full Version : How to concatenate a variable name with i of a for loop.


jhaddington
10-23-2008, 11:58 AM
Hi Guys,

Im reasonably new to AS3 and Im having a slight problem with some syntax of my for loop.

I basically have 5 pictures of scales in different positions so that on a slider it looks to be moving up and down. In order to ensure the movement is smooth I want to load the pictures beforehand, and to minimise code, I have put it in a for loop. The problem I'm having is concatinating the variable "photo" with the "i" of the for loop (see code below). Could anyone point me in the right direction?

In addition to this how would I then call the variable name in the add child below?

Just ask anything that doesn't make sense. :D

Thanks

CODE

for(var i:uint = 0;i>5;i++)
{
var photoi:Loader = new Loader();
photoi.load(new URLRequest("/scales/scale_"+i+".jpg"));
}

function changeHandlerscale(event:SliderEvent):void {
addChild(photoi);
photo.x = 201.9;
photo.y = 131.5;
}

rawmantick
10-23-2008, 12:21 PM
You cannot do so with a variable. But you can do so, if you will give corresponding indexed names to your photos, addChild() them. And then getChildByName("pict"+i);

In your case you just redefine the variable. So no conflict will appear. Just several unnamed loaders.

jhaddington
10-23-2008, 01:31 PM
Thanks for your help romantique. I think I understand what you trying to say, although Im a little confused over the unnamed loaders. Would it be possible for you to retype the for loop so I can see what you mean?! Thanks :)

rawmantick
10-24-2008, 06:07 AM
for( var i:int=0 ; i<10 ; i++ )
{
var loader:Loader = new Loader();
loader.name = "loader_"+i;
addChild(loader);
loader.load(new URLRequest("jsnfjsnfkjdsnfkjsdnf"+i+".jpg");
}

After you can access the corresponding loader like this:
var loader:Loader = getChildByName("loader_"+7) as Loader;