0

I have the following code which works perfectly:

    updaterVariable = 0;
    Tweener.addTween(this, {time:2, transition:"linear", updaterVariable:1000, onUpdate:tweenerUpdate});

I will get the value of updaterVariable tweened from 0 to 1000 in 2 seconds. My question is whether there is a similar way to tween a variable in an array, for example:

    updaterVariable[10] = 0;
    Tweener.addTween(this, {time:2, transition:"linear", updaterVariable[10]:1000, onUpdate:tweenerUpdate});

I tried the above code, and its not working. Can anyone help?

1 Answers1

1

You can pass your array to the tweener and use index as a field to be changed:

updaterVariable[10] = 0;
Tweener.addTween(updaterVariable, {time:2, transition:"linear", '10':1000, onUpdate:tweenerUpdate});

Not sure which tweener you are using, however it works with TweenMax:

private var arr:Array;
public function Main() {
    arr = [];
    arr.push(0);
    arr.push(0);
    arr.push(t);
    TweenMax.to(arr, 1000, { '2' : 1000 } );
    addEventListener(Event.ENTER_FRAME, onframe);
}
private function onframe(e:Event):void {
    trace(arr[2]);//outputs expected numbers
}
www0z0k
  • 4,444
  • 3
  • 27
  • 32
  • Thank you for the answer. But it wont work if I place a variable like "i" in place of "10" right? Is there any way to work it out? – Abhilash K Aug 23 '16 at 07:38
  • Because I may need to change the values around and cant always pass the index as string. – Abhilash K Aug 23 '16 at 07:44
  • @AbhilashK, it depends on the task. You may use a getter function like `public function get i():int { return arr[10]; }` for example. The question is - why do you need to use an array for tweening if you know you need to tween a variable at a certain index? – www0z0k Aug 23 '16 at 07:45
  • @AbhilashK because primitive types are passed by value and `a = 2; arr.push(a); a++; trace(a, arr[0]);` will output `3, 2`, not `3, 3` as it would happen if `arr[0]` stored a reference to `a` – www0z0k Aug 23 '16 at 07:49