A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Problems with 'for' loop

  1. #1

    Problems with 'for' loop

    I'm trying to write a little 'for' loop for the following script. The final script has to load in about 100 variables so want to make it as simple as possible.

    loadLinks = new loadVars();
    loadLinks.load("links.txt");
    loadLinks.onLoad = function() {
    link1 = this.link1;
    link2 = this.link2;
    link3 = this.link3;
    link4 = this.link4;
    link5 = this.link5;
    link6 = this.link6;
    };

    I've managed to get to this, and many other combinations, but it just isn't behaving right.

    for (x=0; x<6; x++) {
    set("link"+x, thislink+x);
    }

    Any ideas where i'm going wrong?

    Thanks,
    Dave.

  2. #2
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    It's:
    code:

    for (x=0; x<6; x++) {
    set("link"+x, this["link"+x]);
    }


    For more details read dynamic references.

    Notice that you're starting at 0, but you have link1 as the first link.

  3. #3
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    you might want to try it this way. I've found the for loops always slow the computer down needlessly. This should do the same with less processing.
    var num:Number = 1;
    if (num < 60){
    set("link"+num, this["link"+num]);
    num += 1;
    }
    .

  4. #4
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi swak,

    What you have writen doesn't make sense.
    A for() loop can't be replaced by an if() just like that.
    code:

    var num:Number = 1;
    if (num<5) {
    trace(num)
    num += 1;
    }


    has nothing to do with
    code:

    for (var num = 1; num<5; num++) {
    trace(num);
    }



    Maybe you were thinking of a while() loop:
    code:

    var num:Number = 1;
    while (num<5) {
    trace(num)
    num += 1;
    }


  5. #5
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    It will amount to the same thing though won't it. It will achieve the same thing. If you look at the description in flash help it says:

    Evaluates the init (initialize) expression once and then starts a looping sequence. The looping sequence begins by evaluating the condition expression. If the condition expression evaluates to true, statement is executed and the next expression is evaluated. The looping sequence then begins again with the evaluation of the condition expression.

    The difference is when it starts. It has the added plus for you being able to chose when it starts the loop.
    .

  6. #6
    Registered Deviant
    Join Date
    Sep 2004
    Location
    Cornelius, OR, USA
    Posts
    280
    A for loop evaluates to this
    Code:
    // testing
    // for( i = 0; i < 60; i++ ) { trace( "hi" ); }
    // becomes
    
    i = 0;
    goto evaluate;
    label: code
    trace( "hi" );
    label: endloop
    i++;
    label: evaluate
    if( i < 60 ) { goto code; }
    label: endif
    Of course, Flash doesn't support code labelling or goto's (yet), but, that's in effect what happens. Your code
    Code:
    if (num < 60){
    set("link"+num, this["link"+num]);
    num += 1;
    }
    has all the makings of a for loop, except for the goto's. Num is only evaluated once, set is run once, num is incremented, but that's it; it stops there. A while loop will do the same thing as a for loop, except that you must place the initializer and incrementer in other places
    Code:
    num = 0;
    while( num < 60 ) {
    	set("link"+num, this["link"+num]);
    	num += 1;
    }
    This is just a matter of preference; personally, I think the for-loop is easier to read. Consider this:
    Code:
    // for-loop
    a = new Array();
    for( i = 0; i < 60; i++ ) {
    	a.push( i );
    }
    
    // if statement
    b = new Array();
    num = 1;
    if( num < 61 ) {
    	b.push( num );
    	num++;
    }
    
    // while loop
    c = new Array();
    num = 1;
    while( num < 61 ) {
    	c.push( num );
    	num++;
    }
    
    trace( a.length );  // gives 60
    trace( b.length );  // gives 1!
    trace( c.length );  // gives 60
    If you can read this, you're in the right place.

  7. #7
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    an if statement takes up less processing. I would know considering I'm running an old computer and the speed makes a difference. I started out with a for loop and it was really choppy after that I changed it to an if loop. Heck there was a few times that the swf ran too slow for even any running. Don't using for loops unless you have to. I can't say anything about while because i don't have any experience.
    .

  8. #8
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    swak the whole point is a simple if statement will only run once for e.g as nunomira quoted above. Try the folllowing example

    Code:
    var num:Number = 1;
    
    if (num<5) {
    
    	trace(num)
    
    	num += 1;
    
    }
    This will only run once not four times as your logic would suggest.
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  9. #9
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    That's why you put it under an onEnterFrame function or clipevent. That's where I get my logic.
    .

  10. #10
    Registered Deviant
    Join Date
    Sep 2004
    Location
    Cornelius, OR, USA
    Posts
    280
    Well, no wonder why your code isn't choppy... putting a single loop in an onEnterFrame would take forever to run. If you wanted to pre-initalize an array of say 60 elements in a movie running at 12fps, it'd take 5 second to get your array setup! Whereas, let's say, a 500 cycle loop running 60 times on a 640 MHz machine would take 4.6875e-5 seconds to accomplish (106,666.667 times faster).

    Computers are designed around performing loops. Operating systems are nothing but giant loops (check to see if this thread needs to run, nope, go on to the next, so forth). Heck, even Flash is a giant loop: start with the first object, update it, go on to the next, update it, when finished with the last object, go back and do it again. If a for-loop is causing your files to slow down then it sounds like either you're running at too high an FPS (anything over 24 is over-kill), you have too many objects on stage (100 objects performing hit tests against all other objects is astronomical), or, as you said above, you're just running too slow of a machine. Of course, we'd like all of our files to play on every computer (heck, I still have a Pentium 90 with 16 megs of ram in use in my office), but sometimes, it's just not feasible. If it's a requirement to get it to run on slower machines (ever read system requirements on the side of games?!) then that's where you learn to trim out excess code, optimize relevant code, or learn to take short cuts (pre-calculate distances before doing hitTests...). What you'll end up finding, is that the best way to optimize code, is usually to put it in a loop!
    If you can read this, you're in the right place.

  11. #11
    Thanks for the all the replies guys. Nunomira's solution hit it on the head straight away, must have been the only combination left that i hadn't tried!

    Cheers man, was giving me brain-ache!

    dave.

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