Groups | Blog | Home
all groups > flash actionscript > may 2004 >

flash actionscript : For loop help!



milkhead
5/19/2004 9:17:15 PM
HI all
First here is my code:

myData = new LoadVars();
myData.load("http://www.starladesign.com/hightest/exist.php");
myData.onLoad = function(){
var num = myData.NumItems;
trace(num);
}
for(n =1; n<num+1 ; n++){
pic = this.createEmptyMovieClip("suba"+n,+n);
pic._x = 20;
pic._y = ((+n-1)*116)+20;
pic.loadMovie("images/suba"+n +".jpg");
trace("images/suba"+n +".jpg");
}

What I am doing is trying to use the number 'num' to be the constraint on the
for loop.
When I trace the 'num' it gives the right number.
I am not sure how to correctly code the first line in the for loop so thatr it
can recognise the 'num' variable
and thus constrain the for loop.
Any help would be much appreciated.
Chris
Jack.
5/19/2004 9:28:53 PM
your loop is trying to use the variable num
before the onLoad has had time to deliver it.
try -

tgt = this;
myData = new LoadVars();
myData.load("http://www.starladesign.com/hightest/exist.php");
myData.onLoad = function(){
var num = myData.NumItems;
trace(num);
for(n =1; n<num+1 ; n++){
pic = tgt.createEmptyMovieClip("suba"+n,+n);
pic._x = 20;
pic._y = ((+n-1)*116)+20;
pic.loadMovie("images/suba"+n +".jpg");
trace("images/suba"+n +".jpg");
}
};



redivider
5/19/2004 9:30:19 PM
If that trace() is giving you the right number, then this should work:

gorlins
5/19/2004 9:37:20 PM
your For loop looks okay, but you have a typo in the second line:

pic = this.createEmptyMovieClip("suba"+n,+n);

should be

pic = this.createEmptyMovieClip("suba"+n, n);

and pic._y = ((+n-1)*116)+20; should be pic._y = ((n-1)*116)+20; as well

Without knowing exactly what your problem is, it's hard to be able to say
anything more than that. I've also never used the notation you use here, so
[quoted text, click to view]

pic = "suba"+n; // Sets up an easy string to reuse
this.createEmptyMovieClip(pic, n);

you can then reference the clip with:

this[pic].whatever, ie this[pic].loadMovie("images/"+ pic +".jpg"); or
this[pic]._x;

Try those and see if your .fla works. Also, maybe put the code in an
onClipEvent(load) function just to insure it's being called.

p.s. this doesn't matter so much, but it's standard in a for loop to go from 0
to < num as opposed to 1 to < num + 1

milkhead
5/19/2004 9:38:01 PM
Thanks guys.
AddThis Social Bookmark Button