/src/com/greensock/plugins/EndArrayPlugin.as
ActionScript | 99 lines | 59 code | 9 blank | 31 comment | 13 complexity | 6d01cbe12d9916c873954f4f1acc07a3 MD5 | raw file
1/**
2 * VERSION: 1.61
3 * DATE: 2010-09-18
4 * ACTIONSCRIPT VERSION: 3.0
5 * UPDATES AND DOCUMENTATION AT: http://www.TweenMax.com
6 **/
7package com.greensock.plugins {
8 import com.greensock.*;
9
10/**
11 * Tweens numbers in an Array. <br /><br />
12 *
13 * <b>USAGE:</b><br /><br />
14 * <code>
15 * import com.greensock.TweenLite; <br />
16 * import com.greensock.plugins.TweenPlugin; <br />
17 * import com.greensock.plugins.EndArrayPlugin; <br />
18 * TweenPlugin.activate([EndArrayPlugin]); //activation is permanent in the SWF, so this line only needs to be run once.<br /><br />
19 *
20 * var myArray:Array = [1,2,3,4];<br />
21 * TweenLite.to(myArray, 1.5, {endArray:[10,20,30,40]}); <br /><br />
22 * </code>
23 *
24 * <b>Copyright 2011, GreenSock. All rights reserved.</b> This work is subject to the terms in <a href="http://www.greensock.com/terms_of_use.html">http://www.greensock.com/terms_of_use.html</a> or for corporate Club GreenSock members, the software agreement that was issued with the corporate membership.
25 *
26 * @author Jack Doyle, jack@greensock.com
27 */
28 public class EndArrayPlugin extends TweenPlugin {
29 /** @private **/
30 public static const API:Number = 1.0; //If the API/Framework for plugins changes in the future, this number helps determine compatibility
31
32 /** @private **/
33 protected var _a:Array;
34 /** @private **/
35 protected var _info:Array = [];
36
37 /** @private **/
38 public function EndArrayPlugin() {
39 super();
40 this.propName = "endArray"; //name of the special property that the plugin should intercept/manage
41 this.overwriteProps = ["endArray"];
42 }
43
44 /** @private **/
45 override public function onInitTween(target:Object, value:*, tween:TweenLite):Boolean {
46 if (!(target is Array) || !(value is Array)) {
47 return false;
48 }
49 init(target as Array, value);
50 return true;
51 }
52
53 /** @private **/
54 public function init(start:Array, end:Array):void {
55 _a = start;
56 var i:int = end.length;
57 while (i--) {
58 if (start[i] != end[i] && start[i] != null) {
59 _info[_info.length] = new ArrayTweenInfo(i, _a[i], end[i] - _a[i]);
60 }
61 }
62 }
63
64 /** @private **/
65 override public function set changeFactor(n:Number):void {
66 var i:int = _info.length, ti:ArrayTweenInfo;
67 if (this.round) {
68 var val:Number;
69 while (i--) {
70 ti = _info[i];
71 val = ti.start + (ti.change * n);
72 if (val > 0) {
73 _a[ti.index] = (val + 0.5) >> 0; //4 times as fast as Math.round()
74 } else {
75 _a[ti.index] = (val - 0.5) >> 0;
76 }
77 }
78 } else {
79 while (i--) {
80 ti = _info[i];
81 _a[ti.index] = ti.start + (ti.change * n);
82 }
83 }
84 }
85
86 }
87}
88
89internal class ArrayTweenInfo {
90 public var index:uint;
91 public var start:Number;
92 public var change:Number;
93
94 public function ArrayTweenInfo(index:uint, start:Number, change:Number) {
95 this.index = index;
96 this.start = start;
97 this.change = change;
98 }
99}