Version 1, last updated by Kirill Mourzenko at September 04, 2008 16:47 UTC
Repeating Jobs
The Handy Toolset has a concept of something called a job that is implemented by com.kirillam.handy.repeaters.HATJob. A job is basically a way to wrap a method of an object in such a way that it gets scheduled to execute at some point in time.
HATJob class provides this API for starting and controlling a job:
// Calls the method repeatedly once every given amount of frames.
repeat( interval : Number, target : Object, method : Function, ... ) : HATJob
// Calls the method repeatedly once every given amount of frames N times.
repeatNTimes( count : Number, interval : Number, target : Object, method : Function, ... ) : HATJob
// Calls the method immediately and then once every given amount of frames.
callAndRepeat( interval : Number, target : Object, method : Function, ... ) : HATJob
// Calls the method N times, immediately and then once every given amount of frames.
callAndRepeatNTimes( count : Number, interval : Number, target : Object, method : Function, ... ) : HATJob
// Calls the method after a certain amout of frames.
callLater( interval : Number, target : Object, method : Function, ... ) : HATJob
// Stops the method from executing at later times.
quit()
// Tells if the job is running.
isActive() : Boolean
The job object itself is passed into the method as the first argument, which can be used inside the method to stop the job from running. Any extra arguments you pass into repeat, repeatNTimes, callAndRepeat, callAndRepeatNTimes and callLater after the specified arguments get passed into your object's method after the job object.
Use HATJob objects instead of using onEnterFrame! HATJob objects are built on top of HATFrameTimer and thus use a single onEnterFrame in the whole Flash movie. HATJob objects also store your object with the method to execute in a weak manner so unless you reference it somewhere it will get garbage collected and the job will stop running.
To make it easier to manager multiple jobs HATObject provides an API for storing them:
// Stores the job using an identifier you supply. Think of this as a variable name.
saveJob( jobId : String, job : HATJob )
// Stores the job anonymously.
saveJob( job : HATJob )
// Removes the job that was stored under the given identifier.
removeJob( jobId : String )
// Removes an anonymous job.
removeJob( job : HATJob )
// Retrieves the job that was stored under the given identifier.
getJob( jobId : String ) : HATJob
// Tells if a job stored under the given identifier is active or not.
isJobActive( jobId : String ) : Boolean
Example use:
// The following code starts a job that traces out "Hello World" on every frame indefinitely.
import com.kirillam.handy.repeaters.HATJob;
var job : HATJob = new HATJob();
job.repeat( 1, this, hello, "Hello World" );
function hello( job : HATJob, message : String )
{
traceI( message );
}