modifying a variable with the Task object

    Apr 09 2006 | 3:32 pm
    I'm trying to modify a variable using the Task object, but even though the value of the variable gets passed to the task when it is first created, it doesn't get updated each time the task runs.
    Here's an example, where I'm trying to smoothly fade the alpha channels of three objects. What am I doing wrong?
    var alpha = [1,1,1];
    function fadeThis(a) { alpha[a] = 1.0 var myTask = new Task(fadeTask,this,alpha[a]); myTask.interval = 10; myTask.repeat(); }
    function fadeTask(anAlpha) { anAlpha -= 0.01 if (anAlpha anAlpha=0; arguments.callee.task.cancel(); } }
    alpha[a] does get set to 0.99 the first time the function is called, but then it just stays there.

    • Apr 10 2006 | 12:48 am
      Hi Perry,
      The following doesn't work since the anAlpha is passed by value, not by reference, as it is a value. You'll need to figure out a different way to either passs as reference(objects are by reference, but numbers are by value). there are a few ways to accomplish this. The quickest/dirtiest way would be to use globals. You could also accomplish by creative use of an object instance, or function properties (you might want to do some web searches on functions and lexical closures for some examples of this last approach).
      In your example, however, it looks like alpha is a global anyway so you could just pass in the alpha array index.
      Hope this helps, Joshua
    • Apr 10 2006 | 3:19 am
      Got it, thanks Joshua. The value/reference distinction always trips me up.