Groups | Blog | Home
all groups > flash actionscript > february 2005 >

flash actionscript : setInterval increasing speed


bobaki1975
2/28/2005 9:46:24 PM
Hi

I'm using this code to move a movie clip across the screen. However, instead
of moving the movieclip every second as hope, it speeds up every second getting
faster and faster. Any idea why?

setInterval (function() {

geckomove.player._x--
updateAfterEvent();
clearInterval();
},1000);

Cheers
Rothrock
2/28/2005 9:56:10 PM
Because you are setting the interval every second. So the first time the code
sets an interval to be called every second for now until it is cleared. At the
end of about 1 second it is called and also called to set it again and so on.
function moveIt(myClip){ myClip._x-- updateAfterEvent(); } moveInterval =
setInterval(moveIt, 1000, geckomove[player]); When you are ready to have the
interval stop. clearInterval(moveInterval); This is all from the top of my
head. I'm away from Flash at the moment, so your milage may vary.
Eric Herlitz
2/28/2005 11:07:39 PM
I assume you put a stop(); in top of the framelayer?

:)


[quoted text, click to view]

Jeckyl
3/1/2005 10:01:04 AM
sound like you're calling the setInterval multiple times.

and because you've gotten the clearInterval wrong (you haven't said what
interval to clear), you'll end up with multiple intervals all running at the
same time and get more and more of them so the object will move faster and
faster.

If you fix your code so it correctly clears the interval, then all is fine.
The nicest way is to attach the interval id to the function itself, so it
knows its own interval.

var mover = function() {
geckomove.player._x--;
updateAfterEvent();
clearInterval(arguments.callee.interval);
}
mover.interval = setInterval (mover,1000);
--
All the best,
Jeckyl

bobaki1975
3/1/2005 10:08:37 AM
Rothrock
3/1/2005 12:47:07 PM
Jeckyl, I've checked out the arguments in the help files, but still don't
understand them very well. How can I poke around in there? If I
trace(arguments.callee); I get [type Function], Same with caller (if the
function is called from another function, but null if called from the root.) I
tried toString and for...in, but wasn't able to find the name of the calling
function. Just curious. I think there could some uses for this. Any guidance
would be great.
NSurveyor
3/1/2005 11:13:42 PM
Rothrock
3/1/2005 11:49:46 PM
Jeckyl
3/2/2005 9:05:04 AM
ok .. arguments is where all the info about the function being called is
stored.

It has the actual arguments to the function as an array .. so you can access
them by position (eg arguments[2]) .. which is useful for when you have
variable numbers of arguments in a function.

It also has a reference to whatever it was that CALLED the function (the
..caller)

And it also has a reference to the function itself (the .callee)

That means, if you add some methods to the function itself, you can access
them via the arguments.callee

This is really useful for storing extra (static) information that the
function needs. Especially for things like onRelease etc that do not take
function arguments ... or for functions called by setInterval, where you
don't know the interval until AFTER you do the setInterval, so you cannot
pass the interval as a function argument.

In the example I had, I created the function and assigned it to the variable
'mover'. Then I created a setInterval for to call that function, which
returns a setInterval id number. Then I put that id number as the
'interval' property of the function, so the function is able to get at that
id number and use it.

Inside the function, I use arguments.callee.interval ... arguments.callee is
the function itself. If I could be sure that the variable 'mover' was still
around, and I wanted to add extra dependency in the code to know that
'mover' is the variable to which the function is assigned, then I COULD have
said 'clearInterval(mover.interval);' .... but that would be BAD, because
making assumptions about the scope of a variable and adding extra
dependencies between pieces of script is BAD design and causes bugs and
maintenance problems. The less assumption and dependencies there are .. the
better and safer and more reusable your code will be !!
--
All the best,
Jeckyl

Jeckyl
3/2/2005 10:42:13 AM
Its always a good day when you learn something new :)
--
All the best,
Jeckyl

AddThis Social Bookmark Button