PDA

View Full Version : Simple for loop help



ad0ac
April 5th, 2007, 08:20 PM
I've been experimenting with a simple imageViewer class, implemented like this:

var viewer1:thumbLoader = new thumbLoader(target_mc);
viewer1.loadImage("whatever.jpg");

my question is how would i use a for loop to make twenty viewers (1-20) load say, twenty elements from an array. One problem is that if I use a string like ["viewer" + i], the class file is expecting a movieclip and throws up a type mismatch. I would guess it's done something like this:

for (var i=0; i<20; i++) {
var this["viewer"+i]:Thumbloader = new thumbLoader("target_mc"+i);
}
but obviously this doesn't work. any help much appreciated.

ad0ac
April 5th, 2007, 08:21 PM
oh yeah, i did try using eval(), but that didn't solve the problem completely either

rabidGadfly
April 5th, 2007, 09:36 PM
Try using:

this["viewer"+i] = new thumbLoader("target_mc"+i);

ad0ac
April 6th, 2007, 07:12 AM
thanks but that doesn't work

rabidGadfly
April 6th, 2007, 08:36 AM
Then you're problem is probably somewhere else whether it be the scope or your code. I'm not familiar with your imageLoader class or the thumbLoader type but I can tell you that the syntax I posted does work.

ad0ac
April 6th, 2007, 11:15 AM
Well in my first post i explained why that wouldn't work - because this["viewer"+i] returns a string and the thumbLoader is expecting a MovieClip. I tried casting but that didn't work either.

rabidGadfly
April 6th, 2007, 01:09 PM
How about if you do this:

var viewer:thumbLoader;
for (var i=0; i<20; i++) {
viewer = new thumbLoader("target_mc"+i);
viewer.loadImage("whatever.jpg");
}