开发者

Flash AS3 for loop query

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"));
ico开发者_开发问答n2.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...


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"));
}


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.


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"));
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜