PDA

View Full Version : setInterval not working


walterppk
05-10-2004, 02:34 PM
I have a button with the following actions script:
on (release) {
if ((_root.sizer < 51)&&(_root.sizer >=0)) {
setInterval (_root.changer(), 5000);
} else {
trace ("value must be between 0 and 50");
}
}


it calls this function:
function changer() {
if (_root.i_grad._currentframe > _root.sizer){ //--go in reverse
for (i = _root.i_grad._currentframe; i >= _root.sizer; i--){
_root.i_grad.gotoAndStop(i);
}
} else { //---- move the movie forward
for (i = _root.i_grad._currentframe; i <= _root.sizer; i++){
_root.i_grad.gotoAndStop(i);
}
}
}

what it is supposed to do is move a bar graph smoothly - but it just goes all in one jump dispite my setinterval() call.

any help please?

tg
05-10-2004, 03:19 PM
is the for loop actually what moves your graph? if so, then comment out the for loop, cause that will loop thru your entire animation the first time the function is called.

try:
commenting out the for loop,
just increment/decrement i depending on your condition,
run your gotoandstop.

walterppk
05-10-2004, 04:49 PM
I can't comment out the for loop since it pushes the movie along frame by frame until it gets to the right spot. like this.

loop
gotoandstop(i);
i (increment or decrement depending on the direction of the movie)
end loop

what i tried at first was this inside the for loop but again the movie seemed to just jump to the frame rather than "frame by frame" to it.
setInterval (_root.i_grad.gotoAndStop(i), 5000);


thanks

stealthelephant
05-10-2004, 05:09 PM
when u call the func the for loop executes until it is finished and u cannot see the motion of the graph cos its happening all at once. So the next time the interval comes around its finished,
use the setinterval to do the loop not the for loop, so remove the for loop
tg is right



on (release) {
if ((_root.sizer < 51)&&(_root.sizer >=0)) {
x = setInterval (_root.changer(), 500); //changed
} else {
trace ("value must be between 0 and 50");
}
}
function changer() { //changed
if (_root.i_grad._currentframe > _root.sizer){ //--go in reverse
_root.i_grad.gotoAndStop(--i);
}
} else if { //---- move the movie forward

_root.i_grad.gotoAndStop(++i);
}
else if(_root.i_grad._currentframe == _root.sizer)
clearInterval(x);
}

tg
05-10-2004, 05:14 PM
thanks stealth, thats what i was getting at.

walterppk
05-11-2004, 12:50 AM
thanks I got an error when I used your code so I condensed it to this but it does nothing - but thanks anyhow

function changer() { //changed
if (_root.i_grad._currentframe > _root.sizer) _root.i_grad.gotoAndStop(--i); //--go in reverse
else if(_root.i_grad._currentframe == _root.sizer) _root.i_grad.gotoAndStop(++i);
else clearInterval(x);
}

button event:
on (release) {
x = setInterval (_root.changer(), 500); //changed
}

stealthelephant
05-11-2004, 07:43 AM
i didnt actually spend a lot of time on it (cos i'm at work :() but that is the just of what u want to do,
want me to explain it in sudo code?

walterppk
05-11-2004, 09:15 AM
i didnt actually spend a lot of time on it (cos i'm at work :() but that is the just of what u want to do,
want me to explain it in sudo code?

That would be greatly appreciated if you could - I hate to be only a taker from this site as I get started on action scripting so could you also recommend some good sites for me to learn actionscripting?

I have PHP 3 yrs javascipt so I understand all concepts such as looping, subroutines/functions, classes and objects/inheritance.

What I need to know is how sytnax works with action scripting constructors because object behavior/constructing is quite different.

thank you kindly! :)

stealthelephant
05-11-2004, 12:27 PM
use as2.0 (mx2004) as its much much closer to javascript

the main thing about AS is it has a time line, this can take getting used to.
what happens basically when use the setInterval is kinda like a thread starting to execute - > the rest of the code will continue executing as normal in parallel with the function being called over and over again.
so with this thread, it will call every x milliseconds for a certain function
eg
x = setInterval(func,parms,500); //x becomes a timeline var when u omitt the var keyword; timeline vars are like global variables in other languages but global vars in AS are slightly different again ;) - more bout that later

so everytime the function is called, perform 1 step i.e. make the graph bigger or smaller depending on what the sizer is
when its finished doing its job use clearInterval(x) to stop that 'thread' from executing

u need to check to see if i have the func call for setinterval correct, i forget the specifics for it

i dont really use AS 1.0 cos it has poxxy OO

walterppk
05-11-2004, 12:43 PM
:) thanks I get in now - yes AS2004MX is very similar - and the good thing here is it is far more exciting then regular programming! :p

stealthelephant
05-11-2004, 02:01 PM
yeah AS sure is a lot of fun and u can get some groovy things up with it - > check out the challenges forum

walterppk
05-11-2004, 04:02 PM
ok it seems so simple but yet I still can not get my gradient bar to retract and expand (play) at a desired speed.

File is posted here if anyone can see what I have done wrong:
http://209.29.131.64/moveBar.zip

function changer() { //changed
//_root.i_grad.gotoAndStop(1);
while (_root.i_grad._currentframe != _root.sizer){
//--play grad bar in reverse
if (_root.i_grad._currentframe > _root.sizer) i = _root.i_grad._currentframe - 1;

//---- move the movie forward
else i = _root.i_grad._currentframe + 1;
_root.i_grad.gotoAndStop(i);
trace ("current frame = " + _root.i_grad._currentframe );
}
clearInterval(_root.tim);
}

stealthelephant
05-11-2004, 04:37 PM
change the while to an if

walterppk
05-11-2004, 04:40 PM
yes but then it will only move the 1 frame it needs to loop again and again (movie 1 frame per loop) until the movie rests at the proper spot.

stealthelephant
05-11-2004, 04:46 PM
it will not move 1 frame per loop with that loop state ment, the loop statement will finish before it moves on to the next frame, i.e. first frame it will be 0 second frame would jump all the way to end loop value

tg
05-11-2004, 04:54 PM
take you code and do this:
1.comment all the code on your button out.
2.modify all code on the main timeline so it looks like this:

function changer() { //changed
//_root.i_grad.gotoAndStop(1);
if (_root.i_grad._currentframe != _root.sizer){
//--play grad bar in reverse
if (_root.i_grad._currentframe > _root.sizer) i = _root.i_grad._currentframe - 1;

//---- move the movie forward
else i = _root.i_grad._currentframe + 1;
_root.i_grad.gotoAndStop(i);
trace ("current frame = " + _root.i_grad._currentframe );
}else{
clearInterval(_root.tim);
}
}

i_change.onRelease=function(){
tim = setInterval(changer,200);
}


this works.... play with the interval (200) to make it go faster or slower. bigger number == slower.

tg
05-11-2004, 04:56 PM
the interval is your loop.

walterppk
05-11-2004, 06:52 PM
the interval is your loop.
works like a charm thank you!!! - I suppose I should have just posted the file in the first place - LOL