A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: function won't perform in loop: HELP!

  1. #1
    Junior Member
    Join Date
    Aug 2004
    Posts
    1

    function won't perform in loop: HELP!

    Hello!

    I've been battling this Actionscript problem for quite some time. I can't get a function to accept the values from the loop it's contained in. I have a loadVars() function in a for loop that reads external data. However, using traces, the output shows that the loops goes through i=1, i<=5 but then once it hits the function, i gets bumped up to 6! Also, the function only executes the this last number. The output window displays this:

    1
    2
    3
    4
    5
    6
    6
    6
    6
    6

    Any help would be very much appreciated! Thank you!

    --kevin

    PS: Here's my code:

    tgt = this;
    function quakePlot() {
    for (var iv = 1; iv<=5; iv++) {
    trace(iv);
    tgt["loadVarsCoords"+iv] = new loadVars();
    tgt["loadVarsCoords"+iv].load("quake_coords_"+iv+".txt");
    tgt["loadVarsCoords"+iv].onLoad = function(success) {
    if (success) {
    var coords, myText, xspot1, xspot2, yspot1, yspot2, xwidth, yheight;

    myText = tgt["loadVarsCoords"+iv].coords;
    myTextArray = myText.split(",");
    //trace(myText);
    xspot1 = myTextArray[0];
    //trace(myTextArray[0]);
    xspot2 = myTextArray[2];
    //trace(myTextArray[2]);
    yspot1 = myTextArray[1];
    //trace(myTextArray[1]);
    yspot2 = myTextArray[3];
    //trace(myTextArray[3]);
    dupeme.duplicateMovieClip("dupeme"+iv);
    trace(iv);
    setProperty("dupeme"+iv, _x, xspot1);
    setProperty("dupeme"+iv, _y, yspot1);
    xwidth = (xspot2-xspot1)*3;
    //trace(xwidth);
    yheight = (yspot2-yspot1)*3;
    //trace(yheight);
    setProperty("dupeme"+iv, _width, xwidth);
    setProperty("dupeme"+iv, _height, yheight);
    //trace("WOO-WOO-WOOO");
    }
    };
    }
    //trace(i);
    }
    setInterval(quakePlot, 1000);

  2. #2
    proud new daddy! LuxFX's Avatar
    Join Date
    May 2000
    Location
    Dunsinane
    Posts
    1,148
    The problem is in two parts:

    1. First off, loading variables is what's called an "asynchronous" function, which means that flash will continue to run actionscript without waiting for the loading to take place. In your instance, flash will probably complete all of the loops in your interval before the variables are actually returned.

    (more info on loading variables here)

    2. The other half of the problem is that you are declaring tgt["loadVarsCoords"+iv] inside the for loop, which makes it a local variable. It will literally disappear as soon as the for loop finishes! That's why your function isn't calling -- it doesn't exist anymore.

    You should be able to clear up the whole thing by simply creating the tgt variable at either _root.tgt["loadVarsCoords"+iv] or _global.tgt["loadVarsCoords"+iv]. This will ensure that the variable is created outside of the local scope and into one that is globally accessible. Just remember to keep referring to it at _root or _global for the rest of the function.
    For War's a banker, flesh his gold. There by the furnace of Troy's field, Where thrust meets thrust, he sits to hold His scale, and watch the spear-point sway; And back to waiting homes he sends Slag from the ore, a little dust To drain hot tears from hearts of friends

    - Aeschylus, Agamemnon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center