PDA

View Full Version : For loop counter not resetting ...



TylerNZ
June 16th, 2009, 05:02 PM
Hi, so I have a class method that contains a for loop ... however, each time I call the class method, the for loop counter continues to count from where it left off! Agh - can anyone help me make sure the loop counter get's reset?

//sliders.length = 5

private function test():void
{
var i:Number = 0;
for (i=0; i<sliders.length; i++)
{
var time:Number = i*0.5;
trace('i:'+i+' | time:'+time);
}
}

tmp.test();
tmp.test();

This traces:
i:0 | time:0
i:1 | time:0.5
i:2 | time:1
i:3 | time:1.5
i:4 | time:2

i:5 | time:2.5
i:6 | time:3
i:7 | time:3.5
i:8 | time:4
i:9 | time:4.5

xchrisx
June 16th, 2009, 05:28 PM
That doesn't make a whole lot of sense.

Anyway, you could try resetting the var in the for loop like this, and NOT declaring it in the function:


for (var i:Number=0; i<sliders.length; i++) { ... etc

TylerNZ
June 16th, 2009, 05:33 PM
I've tried that too - same result ... does it have anything to do with the variable scope within the class method?!???


That doesn't make a whole lot of sense.

Anyway, you could try resetting the var in the for loop like this, and NOT declaring it in the function:


for (var i:Number=0; i<sliders.length; i++) { ... etc

xchrisx
June 16th, 2009, 08:23 PM
Yes, that's my hunch too. If you put the entire code on a root timeline and take out the private and the tmp then it all works fine like it's supposed to. My guess is that your class is holding on to the var for some reason. Do you see a duplicate variable warning? Are you sure sliders.length is only 4? Try changing some of the variables to constants until you pinpoint the problem.

of course, you could hack it by manually resetting the var, ie: if (i==sliders.length) i=0; but i don't recommend it -- you have a problem that should be dealt with before it creates a real headache.

TylerNZ
June 17th, 2009, 10:54 AM
The variable set within a class method, should only be within the scope of that method, right? I haven't tested to see if I can read the variable outside of that method (I'll go do some playing now) ... but I just don't understand why it's not resetting the variable every time that method is called.

.ral:cr
June 17th, 2009, 12:42 PM
that's why is useful the trace method that someone posted it as the most unuseful somewhere here.
i'd do a trace for every line of code, to see step by step what's happening.

my concern is what are you calling there. tmp is an instance of the class that contains your test method? and how are you calling it if test is a private method?

i've tested your code in haxe(i put it randomly into a project i'm working now) and everything is fine:

Frontul.hx:58 : i:0 | time:0
Frontul.hx:58 : i:1 | time:0.5
Frontul.hx:58 : i:2 | time:1
Frontul.hx:58 : i:3 | time:1.5
Frontul.hx:58 : i:0 | time:0
Frontul.hx:58 : i:1 | time:0.5
Frontul.hx:58 : i:2 | time:1
Frontul.hx:58 : i:3 | time:1.5

TylerNZ
June 17th, 2009, 12:48 PM
I've attached the class file - this is my very first AS3 Class - so there's prob. a ton of redundancy. hehe. The specific line this issue is on, is: 92

Only look if you want to dig deeper ... :)

.ral:cr
June 17th, 2009, 01:00 PM
hmm, and how exactly are you noticing a bug? because i see a mistype, it's sliders not slider. and you can simplify the code this way


for (var i:Number = 0; i < sliders.length; i++)
TweenLite.to(sliders[i],1,{x:20,ease:Quint.easeOut,delay:i*0.3});

btw, didn't know that you can push more elements int-o an array separated by comma, it's really working?

TylerNZ
June 17th, 2009, 01:07 PM
Oops - I quickly re-typed up the for-loop. I had given up and went a more manual way ...

Attached is the resulting .swf - If you keep going between "Calculate" and "Recalculate" - you'll notice it takes longer and longer for the slider elements to come in. That's because variable 'i' is getting larger and larger ....

waaa? you can't upload a swf here? WEIRD. Ok, you can see it here: http://joelataylor.com/zees/flash/SleepCalculator.swf

.ral:cr
June 17th, 2009, 01:12 PM
i've got it, it's because you push the elements in the array over and over

try:
sliders = [sliderSleep, sliderFallAsleep, sliderWakeUpOften, sliderStayAwake, sliderWakeUp];

TylerNZ
June 17th, 2009, 01:14 PM
THANK YOU!!!!! - haha, silly me. You're totally right, I was continually adding the same elements to the array. Thanks again!