Watcher
| Kind of class: | class |
|---|---|
| Inherits from: | none |
| Author: | Arthur Clemens |
| Classpath: | org.asapframework.util.watch.Watcher |
| File last modified: | Saturday, 14 October 2006, 23:02:33 |
The standard
Watcher tries to match a variable or a function return value to a given property value. For instance, Watcher may be used to call a callback function when the mouse is over a certain screen region.
Watching may be a one-time hit, or perpetuously until the value is met or the Watcher is stopped.
Object.watch() function in Flash cannot watch getter/setter properties. From the help text:Generally, ActionScript predefined properties, such as _x, _y, _width and _height, are getter/setter properties, and thus cannot be watched with Object.watch().The current Watcher class can be used to watch any variable or method return value. But the mechanism used is less efficient than
Object.watch(), as it is based on setInterval calls to evaluate the variables. The interval frequency is passed in the constructor to allow a slower evaluation (thus better performance) where possible. But you will see real performance problems only with a fast frequency interval.Watcher tries to match a variable or a function return value to a given property value. For instance, Watcher may be used to call a callback function when the mouse is over a certain screen region.
Watching may be a one-time hit, or perpetuously until the value is met or the Watcher is stopped.
Usage:
- The following example shows how to check for value of the MovieClip's
_alphavariable; when_alphahas gone to 0, the method 'afterZero' is called. Checking is done each 12th of a second and perpetuously until the clips' _alpha has turned to 0.var watcher:Watcher = new Watcher(my_mc, "_alpha", 0, 1/12); watcher.setAfterMethod(this, "afterZero"); watcher.start();
A test to make the clip fade out:var queue:ActionQueue = new ActionQueue(); queue.addAQMethod (AQFade.fade, my_mc, 1, null, 0 ); queue.run();
To do:
- For the conditional value add logic such as "<= 50" or "!= 0".
Summary
Constructor
- Watcher (inWatchedObject:Object, inWatchedMethodOrVar:Object, inConditionalValue:Object, inIntervalDuration:Number, inShouldRepeat:Boolean)
- Sets up the Watcher.
Instance properties
- mIval : Number
- mIntervalDuration : Number
- mWatchedObject : Object
- mWatchedMethodOrVar : Object
- mCallbackObject : Object
- mCallbackMethod : Object
- mCallbackParams : Array
- mAfterObject : Object
- mAfterMethod : Object
- mAfterParams : Array
- mConditionalValue : Object
- mShouldRepeat : Boolean
Instance methods
- start : Void
- Starts the Watcher timer.
- stop : Void
- Stops the Watcher timer.
- restart : Void
- Restarts the Watcher timer.
- setIntervalDuration (inIntervalDuration:Number) : Void
- Sets the Watcher timer frequency interval.
- setCallback (inCallbackObject:Object, inCallbackMethod:Object) : Void
- Sets a function (object and function) that should be called during watching.
- setAfterMethod (inAfterObject:Object, inAfterMethod:Object) : Void
- Sets a function (object and function) that should be called if the variable to watch has met the condition as set in inConditionalValue in the Watcher constructor.
- setVariableToWatch (inWatchedMethodOrVar:Object) : Void
- Stores the variable to watch.
- die : Void
- Cleans up the Watcher object by calling clearInterval.
- isBusy : Boolean
- Returns if the Watcher is still running the interval function.
- toString : String
- WAStartTimer : Void
- WAStopTimer : Void
- WAUpdateTimer : Void
- WACheck : Void
Constructor
Watcher
function Watcher (
inWatchedObject:Object,
inWatchedMethodOrVar:Object,
inConditionalValue:Object,
inIntervalDuration:Number,
inShouldRepeat:Boolean)
Sets up the Watcher.
Pass a variable to watch the value of, or a function to watch its return value. After the given interval duration, the variable is evaluated; if the checked variable is equal to the passed 'inConditionalValue' value, a callback function is called and the watcher stopped.
Pass additional arguments to the callback method as a comma-separated list.
Pass a variable to watch the value of, or a function to watch its return value. After the given interval duration, the variable is evaluated; if the checked variable is equal to the passed 'inConditionalValue' value, a callback function is called and the watcher stopped.
Pass additional arguments to the callback method as a comma-separated list.
Parameters:
inWatchedObject :
object that owns the variable to watch
inWatchedMethodOrVar:
method name or function reference of function that returns a variable or variable name
inConditionalValue :
value that the variable to watch is tested against
inIntervalDuration :
time duration between each check; duration in seconds
inShouldRepeat :
(optional) the variable should be checked only once (false) or repeatedly (true); default true
- value false: check only once; if the checked variable is not equal to the passed inConditionalValue value, nothing happens
- value true: check repeatedly until the checked variable is equal to the passed inConditionalValue value, or until stop is called
Example:
- First set up our test environment:
var family:Object = new Object(); family.memberCount = 2; family.birth = function () { family.memberCount++; } family.getMemberCount = function () { return family.memberCount; } var birthControl:Object = new Object(); birthControl.act = function () { trace("act"); }
The watcher that is created in the next line will watch for the value returned from 'getMemberCount' from object 'family'. When getMemberCount returns 5, method 'act' of object 'birthControl' is called. Checking is done 31 times per second, until the value is found.var watcher:Watcher = new Watcher(family, "getMemberCount", 5, 1/31); watcher.setAfterMethod(birthControl, "act"); watcher.start(); family.birth(); family.birth(); family.birth();
Instance properties
mAfterMethod
private mAfterMethod:Object
(read)
mAfterObject
private mAfterObject:Object
(read)
mAfterParams
private mAfterParams:Array
(read)
mCallbackMethod
private mCallbackMethod:Object
(read)
mCallbackObject
private mCallbackObject:Object
(read)
mCallbackParams
private mCallbackParams:Array
(read)
mConditionalValue
private mConditionalValue:Object
(read)
mIntervalDuration
private mIntervalDuration:Number
(read)
mIval
private mIval:Number
(read)
mShouldRepeat
private mShouldRepeat:Boolean
(read)
mWatchedMethodOrVar
private mWatchedMethodOrVar:Object
(read)
mWatchedObject
private mWatchedObject:Object
(read)
Instance methods
die
function die (
) : Void
Cleans up the Watcher object by calling clearInterval.
isBusy
function isBusy (
) : Boolean
Returns if the Watcher is still running the interval function.
Returns:
- True: the Watcher is still running; false: the Watcher has stopped.
setAfterMethod
function setAfterMethod (
inAfterObject:Object,
inAfterMethod:Object) : Void
Sets a function (object and function) that should be called if the variable to watch has met the condition as set in
inConditionalValue in the Watcher constructor. Parameters:
inAfterObject:
object of the method that should be called
inAfterMethod:
Method (name or function reference) to be called. Parameters that should be passed to the 'after' method can be appended as a comma separated list.
setCallback
function setCallback (
inCallbackObject:Object,
inCallbackMethod:Object) : Void
Sets a function (object and function) that should be called during watching. The function will be passed the current value of variable that is being watched.
Parameters:
inCallbackObject:
object of the method that should be called during watching
inCallbackMethod:
Method (name or function reference) to be called during watching. Parameters that should be passed to the callback method can be appended as a comma separated list.
setIntervalDuration
function setIntervalDuration (
inIntervalDuration:Number) : Void
Sets the Watcher timer frequency interval.
Parameters:
inIntervalDuration:
time duration between each check; duration in seconds
Example:
-
var watcher:Watcher = new Watcher(); watcher.setIntervalDuration(1/12);
setVariableToWatch
function setVariableToWatch (
inWatchedMethodOrVar:Object) : Void
Stores the variable to watch.
Parameters:
inWatchedMethodOrVar:
method name or function reference of function that returns a variable or variable name
toString
function toString (
) : String
WACheck
function WACheck (
) : Void
WAStartTimer
private function WAStartTimer (
) : Void
WAStopTimer
private function WAStopTimer (
) : Void
WAUpdateTimer
private function WAUpdateTimer (
) : Void