A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Making a function run when a variable is changed.

  1. #1
    Member
    Join Date
    Jun 2000
    Posts
    63

    Making a function run when a variable is changed.

    Code:
    //==============================================================================================//
    //Create Local Connection with other SWF's
    //==============================================================================================//
    _global.topmenu_lc = new LocalConnection();
    _global.topmenu_lc.GetMenuArea = function(param) {
    	XMLMenu = param;
    };
    _global.topmenu_lc.connect("incoming");
    //
    //==============================================================================================//
    //Master Variables at beginning runtime.
    //==============================================================================================//
    XMLMenu = "soo.xml";

    What I need is an efficient method to watch the XMLMenu variable so that when it is changed...... it will run this function.

    Code:
    //==============================================================================================//
    //Rebuild Menu Fuction
    //==============================================================================================//
    MovieClip.prototype.RebuildMenu = function() {
    	//
    	this.LastMainButton = this.menuItems_arr.length;
    	this.ResetButton = this.LastMainButton-1;
    	// ---------- Loop all the Main Buttons Shut
    	for (var k = 0; k<Number(this.LastMainButton); k++) {
    		var ObjectPath = this["MainButton"+k];
    		var twnobj = ObjectPath;
    		var twnprop = "_y";
    		var twnbegin = ObjectPath._y;
    		var twnfinish = ObjectPath.MainBtnOrigin;
    		this.PositionMainButton_twn = new Tween(twnobj, twnprop, TT, twnbegin, twnfinish, Speed);
    		this.PositionMainButton_twn.stop();
    		this.PositionMainButton_twn.continueTo(twnfinish);
    		this.PositionMainButton_twn.addListener(ObjectPath);
    		if (k<Number(this.ResetButton)) {
    			ObjectPath.onMotionFinished = function() {
    				this.removeMovieClip();
    			};
    		} else {
    			ObjectPath.onMotionFinished = function() {
    				getXML(XMLMenu);
    			};
    		}
    	}
    };
    //
    As soon as this swf file is sent a new variable via the Local connection I want it to automatically rebuild the menu. Really need to try and sort this out ASAP so help is appreciated guys.

  2. #2
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    Object.watch?

  3. #3
    Member
    Join Date
    Jun 2000
    Posts
    63
    Thats what I would have assumed Senocular but I can't understand how I would do it when not getting a changing the property of the variable/object yet wanting it to go on the change of the actual variable. property of anything.

    XMLMenu.watch(RebuildMenu());


    Does not work. Can you show me with my example Sen?

  4. #4
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    well I dont know what you're doing exactly (and I really dont feel like going through the code ) so Ill just spit off some stuff about Object.watch and see if it does what you think you need it to.

    What object watch does is lets you check to see when a variable changes within an object. This object can be an object youve made, any Flash made object or a movieclip; even _root. The variable it checks has to be a 'normal', so to speak variable, and nothing added through object.addproperty or anything like _x and _y props. Basically, you're safe if youve just made a generic variable yourself and have been giving it different values like true, 3 or "stuff" etc.

    Basically you can think of a watcher as an interceptor of the = sign when used with the variable youre watching. Lets say we're working with the variable myVar and we want to watch it. We set up the watcher (explained below) and anytime thereafter, within your flash movie (until unwatched) when you have myVar = ... the watcher command takes over and intercepts that assignment calling the watcher callback function to do its thing.

    For setting up a watcher, you need to use the following format

    ObjectToWatch.watch("variableToWatch", onVariableChangedFunction, arg (<- optional));

    the onVariableChangedFunction actually takes place of the new value you are trying to assign he variable. For example, if you have

    myVar = 0;
    checkMyVar = function(){ trace("changing myVar"); }
    this.watch("myVar", checkMyVar);

    whenever you try to assign myVar to be anything like:

    myVar = 2;

    The 2 is more or less replaced with the checkMyVar function (the onVariableChangedFunction) and myVar is assigned to the result. In this case it becomes nothing (well undefined) because checkMyVar doesnt return anything. However, the onVariableChangedFunction gets passed to it some arguments to help control the final returned result, afterall, you propably dont want your variable to become undefined when you try to assign it a value. So in the onVariableChangedFunction (btw this is also called the 'callback' function, Im just trying to use a name that makes more sense) you get passed 4 arguments:
    (variableName, oldVariableValue, newlyAssignedValue, optionalArgument)
    in the case of the previous example, variableName is "myVar", oldVariableValue is 0 and newlyAssignedValue is 2. There is no optionalArgument because I didnt pass anything in the watch call which defines that.

    So to redefine the checkMyVar, you can say:
    checkMyVar = function(varName, oldVal, newVal){
    trace("changing "+varName+" from "+oldVal+" to "+newVal);
    return newVal;
    }

    then if you ever change myVar (and this is anywhere, as long its the myVar in the object which the watch was used) then the trace
    "changing myVar from 0 to 2"
    will be run and myVar will be changed from 0 to 2 (in the case that it was 0 before and then set to 2)

    So watchers let you do a couple of things. It lets check to see when a variable is ever assigned to a new value, but it also lets you determine if the variable will actually get assigned the new value alltogether. You can just as well return the old value and the variable will stay the same. This can even be determined within the function using an if/else etc. In a loose sense it can almost act as a way to override the = operator, letting you instead of = meaning an assignment for a certain variable, you can actually have it represent something like a sin of the number. example:

    myObj = {}
    myObj.sinValue = 0;
    assignSine = function(name, oldVal, newVal){
    return Math.sin(newVal);
    }
    myObj.watch("sinValue", assignSine);

    with (myObj){
    sinValue = Math.PI/2;
    trace(sinValue); // traces 1 since Math.sin(Math.PI/2) == 1
    }

    So assuming all fits well with your menu thing, if you just have one variable somewhere that gets changed somewhere else, you can find out when that gets changed and then run a function to do something about it. And if not, then at least you know a little something new about watchers.

  5. #5
    Member
    Join Date
    Jun 2000
    Posts
    63
    Hi Senocular, I could not get the watch to work as i had hoped...but instead I thought why not just call the rebuild function in the LC function. The only time the Rebuild function need to be called is when the LC function is run, so it is now workign quite well...Still some bugs as such but I got a working model currently

    Thanx for taking the time to respond

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center