Detect when a variable changes in flash

Here is a nifty little script to keep track of variables. Basically you declare a variable, create a callback to be fired when the variable changes, and then you "watch" it.
// Declare your variable (can be any kind)
var myVar:Number = 0;

// Here's your callback that will be fired every time you change your var
myVarChangeCallBack = function (varName, oldval, newval):String {
	trace ('Variable "' + varName + '" has changed from ' + oldval + ' to ' + newval);
	return newval;
};

// Watch your var for changes
watch("myVar", myVarChangeCallBack);


// Some code to try this out
_root.onMouseDown = function () {
	myVar++;
}
Leave a comment
Name
Email (optional)
Your email will only be used to show your Gravatar and to send you replies to this article (if you opt to receive them). You won't get ANY other messages at all!
Comment
No HTML allowed
Notify me via email
 
 
Share

Valid HTML 4.01 Strict