-
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.
-
Registered User
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.
-
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;
}
-
Registered User
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;
}
-
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.
-
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.
-
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.
-
Flashmatics
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.
-
That's why you put it under an onEnterFrame function or clipevent. That's where I get my logic.
-
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.
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|