setInterval in ActionScript 2 gotcha
Here's a quick ActionScript 2.0 tip for anyone using the setInterval method within a class. If you follow the standard usage guidelines of the function, you would normally include two parameters -- the function to call when the interval is over, how many milliseconds you want to wait between calls, plus any additional parameters the called function should receive. Normally setInterval would look like this (when neatly assigned to a variable):
var myInterval = setInterval(callMe,1000);
If you were to include this in an AS2 class, you would expect it to work. Why? Because setInterval is assigned to the variable myInterval which is an attribute of the parent class, right alongside callMe which is a method of the same parent class. It's like having two students sitting next to one another in the classroom -- they should easily see one another. But they don't.
It turns out (after some hair pulling on my part) that setInterval, using the standard notation, cannot see respective class members unless you tell it where to look. You must identify the class to setInterval for it to know where to look for callMe. So in an AS2 class, you would add the this identifier to the beginning:
var myInterval = setInterval(this,"callMe",1000);
By adding this, the function knows where to look for callMe. Which, for those with sharp eyes, will note has also changed. callMe must be wrapped in quotes and treated as a string.
Stopping the interval, fortunately, is exactly the same as before:
clearInterval(myInterval);
This threw me for a bit of a loop last night, so I thought I'd note it here in case other Flash developers find themselves in a similar pickle.
