0

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
  • 1
    Don't really know AS's syntax that well, but you're missing a '.' in the "jpg" bit. :-) Commented Jan 21, 2010 at 16:40
  • In general, ask yourself: Is saving typing out or copy/pasting these lines worth the loss of flexibility or structural changes to my program? The loop idea you suggest forces you into certain naming conventions, which may be good or may be bad. But I consider 'saving on code' to be a non-issue compared to maintainability and flexibility. Commented Jan 21, 2010 at 17:26

3 Answers 3

2

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"));
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.