PDA

View Full Version : variable change listener?



emlynmiles
April 17th, 2005, 05:36 PM
Hi All,

I'm just wondering if there is a way to listen to a variable for changes? I've seen that you can listen to a change in a component, but I've not found any examples of variables, is it possible?

This is what I have tried, to no avail....


var myVariable:Number = 0;

var myListener = new Object();
myListener.change = function(change) {
trace("variable changed");
};
myVariable.addEventListener("change", myListener);

I'm getting this error...

---------------------------------------------
**Error** Scene=Scene 1, layer=Layer 2, frame=1:Line 188: Syntax error.
***myListener.change = function(change) {

Total ActionScript Errors: 1 Reported Errors: 1
---------------------------------------------

I have also tried .onChange instead of just .change but get the same results.

I'm a flash newbie and I could really do with your help, thanks in advance. Emlyn

rhamej
April 17th, 2005, 11:43 PM
Just put it on an onEnterFrame event in a blank mc somewhere on the stage.
ie:
if ( myVar==diffVar){
do what ever
}

claudio
April 18th, 2005, 07:37 AM
Use the Object.watch method.

var myVar = false;
this.onMouseDown = function() {
myVar = !myVar;
};
function watchMyVar(prop, oldVal, newVal) {
if (oldVal != newVal) {
trace(prop+" changed to "+newVal);
}
return newVal;
}
this.watch("myVar", watchMyVar);
Key.addListener(this);