views:

96

answers:

3

I was hoping for a way that I could save on code by creating a loop for a few lines of code. Let me explain a little, with-out loop:

icon1.button.iconLoad.load(new URLRequest("icons/icon1.jpg"));
icon2.button.iconLoad.load(new URLRequest("icons/icon2.jpg"));
icon3.button.iconLoad.load(new URLRequest("icons/icon3.jpg"));
icon4.button.iconLoad.load(new URLRequest("icons/icon4.jpg"));

etc... But with a loop could I have something like:

for (var i:uint = 0; i < 4; i++) {  
    icon+i+.button.iconLoad.load(new URLRequest("icons/icon"+i+"jpg"));
}

Any ideas welcome...

+2  A: 

In AS2 it would be something like this:

for (var i = 1; i <= 4; i++) {  
    this["icon"+i].button.iconLoad.load(new URLRequest("icons/icon"+i+".jpg"));
}
Richard Inglis
+1  A: 

I would do something like this:

import flash.utils.Dictionary;

var iconDict:Dictionary = new Dictionary();
iconDict[icon1] = "icons/icon1.jpg";
iconDict[icon2] = "icons/icon2.jpg";
iconDict[icon3] = "icons/icon3.jpg";
iconDict[icon4] = "icons/icon4.jpg";

for (key:Object in iconDict)
{
    key.button.iconLoad.load(new URLRequest( iconDict[key] ));
}

This allows you to call your icon objects whatever you like, as well as the actual icon graphics whatever you like.

There is some documentation on Dictionary here.

Ipsquiggle
A: 

If those icons are already children of the object which contains the code you're writing, and their instance names have been set using the Flash IDE, then you could do this.

var icon_count:int = 4;
for(var i:int = 0; i < icon_count; ++i)
{
    getChildByName("icon" + i).button.iconLoad.load(new URLRequest("icons/icon" + i + ".jpg"));
} 
danyal