While loop + dupMC confusion

maybe I haven’t had enough coffee yet, but this is throwing me for a loop (bad pun :P).

My confusion lies with the “myname” var. If I declare it outside of the loop, the loop does not work correctly, but if I declare it inside the loop it works fine. I guess I don’t get why “mycount” is incremented, and works, being declared outside of the loop, but “myname” does not get incremented unless its inside the loop. Is this just the way dupMC works?

[AS]amount=0;
mycount = 1;
xmoveNum = 30;
ymoveNum = 0;

while (amount<3) {
myname = “MC”+mycount; // <-here
duplicateMovieClip(myMC, myname, mycount);
setProperty (myname, _x, xmove);
setProperty (myname, _y, ymove);
mycount++;
amount++;
xmove += xmoveNum;
ymove += ymoveNum;
}
[/AS]

Think logically. This is executed everytime until amount is equal as or higher than 3. And, it adds 1 to mycount every time. Now have a look at the line:

myname = “MC”+mycount;

It will do this also three times, but with mycount being different every time, since we add 1 every time. It will also set it’s values every time:

setProperty (myname, _x, xmove);
setProperty (myname, _y, ymove);

So if you’d place that code (
myname = “MC”+mycount;
setProperty (myname, _x, xmove);
setProperty (myname, _y, ymove);
) outside the loop, it will only do this once. Do you understand ?

I understand what the code does, I am just confused about the scope of the two variables myname and mycount.

I guess I don’t understand why the increments to mycount, aren’t reflected in myname when declaring it outside the loop, (see “A” below). Why on the second loop is myname still MC1?

amount=0;
mycount = 1;
xmoveNum = 30;
ymoveNum = 0;
myname = "MC"+mycount; // <-here "A"

while (amount<3) {
        myname = "MC"+mycount; // <-or here "B"
        duplicateMovieClip(myMC, myname, mycount);
        setProperty (myname, _x, xmove);
        setProperty (myname, _y, ymove);
        mycount++;
        amount++;
        xmove += xmoveNum;
        ymove += ymoveNum;
}

Originally posted by jeremy *
I guess I don’t understand why the increments to mycount, aren’t reflected in myname when declaring it outside the loop

Because mycount is incremented inside the loop, and you used “MC”+mycount before the loop.

[AS]
amount=0;
mycount = 1;
xmoveNum = 30;
ymoveNum = 0;
myname = “MC”+mycount; // <-here “A”
//At this point, mycount is 1 so myname will be “MC1” here.

while (amount<3) {
myname = “MC”+mycount; // <-or here “B”
//First time: mycount is still 1. So myname will be “MC1”
//Second time: mycount is 2. So myname will be “MC2”
//Third time: mycount is 3. So myname will be “MC3”
duplicateMovieClip(myMC, myname, mycount);
setProperty (myname, _x, xmove);
setProperty (myname, _y, ymove);
mycount++;
//First time: mycount is now 2
//Second time: mycount is now 3
//Third time: mycount is now 4
amount++;
//First time: amount is now 1
//Second time: amount is now 2
//Third time: amount is now 3, so not longer <3 so the loop will stop.
xmove += xmoveNum;
ymove += ymoveNum;
}

//At this point, myname would be “MC4”
[/AS]

I appreciate your help on this, thank you!.

So once a loop is “looping” it no longer pulls values from outside the loop, it just stores the values initially set in the loop’s first pass, and then edits those? Is that right?

Well I don’t know if the loop keeps a copy, edits that one, and palces it back, (that is what you meant right ?) but I can tell you it can edit values from outside the loop. Place this code on the first frame of a new file, and test it, you’ll see:

[AS]
mycount=0
trace("Mycount before: "+mycount)
while(mycount<3){
mycount++
trace("Mycount in the loop: "+mycount)
}
trace("Mycount after the loop: "+mycount)
[/AS]