flash actionscript:
I have a rather large array of variables with constantly changing values that I need to add. The variables are like this for example. pop1=75 pop2=125 pop3=60 pop4=515 (note these variable amounts are constantly changing) and on and on, there will be over 2000 variables. As these variables are constantly changing in value I need to be able to add them Instead of simply adding the variables like population = ((pop1) + (pop2) + (pop3) and on and on I was pretty sure that i could use a loop to do this Something like this.onEnterFrame = function() { pop1=75 pop2=125 pop3=60 pop4=515 for(i = 0; i <= 2050; i++) { population = (add up all of the variable amounts); } I have researched this and I seem to be too dumb to put the material together to know how to add the variables in a loop. Any help would be appreciated.
try this var pop = new Array(); pop[1] = 10; pop[2] = 73; pop[3] = 92; .... var sum = 0; for (i in pop) { sum += pop[i];
Close, but there is no end to the loop. If I do not put the code in an onEnterFrame function it works. But in the onEnterFrame function the code goes on and on. To explain my code better it goes like this. this.onEnterFrame = function() { if (p0._x<=5 && ph._y >=25 && pw._x >=25 && p0._y <= 5 && ph._x <5 && pw._y <5) { pop[1]=50; }else{pop[1]=0}; //code for pop[2 if (p0._x<=25 && ph._y >=25 && pw._x >=50 && p0._y <= 5 && ph._x <25 && pw._y <5) { pop[2]=75; }else{pop[2]=0}; if (p0._x<=50 && ph._y >=25 && pw._x >=75 && p0._y <= 5 && ph._x <75 && pw._y <5) { pop[3]=45; }else{pop[3]=0}; var sum = 0;for (i in pop) {sum += pop;} }
Of course it won't work in an onEnterFrame... it is constantly executing... and never gets to finish the first calculation before it starts the second and third and all subsequent calculations... thus continually starting again and never giving you a result. It would be different if it was only handling a couple of calculations... it could probably handle that in the 1/8th of a second that it has... but 2000 ?? Not a chance... So in answer to your question, whilst your code is ostensibly correct, it won't work as you want. sorry, cheers,
Don't see what you're looking for? Try a search.
|