/src/com/greensock/plugins/EndArrayPlugin.as

https://bitbucket.org/HopeSky/mars_nd2d · 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. **/
  7. package com.greensock.plugins {
  8. import com.greensock.*;
  9. /**
  10. * Tweens numbers in an Array. <br /><br />
  11. *
  12. * <b>USAGE:</b><br /><br />
  13. * <code>
  14. * import com.greensock.TweenLite; <br />
  15. * import com.greensock.plugins.TweenPlugin; <br />
  16. * import com.greensock.plugins.EndArrayPlugin; <br />
  17. * TweenPlugin.activate([EndArrayPlugin]); //activation is permanent in the SWF, so this line only needs to be run once.<br /><br />
  18. *
  19. * var myArray:Array = [1,2,3,4];<br />
  20. * TweenLite.to(myArray, 1.5, {endArray:[10,20,30,40]}); <br /><br />
  21. * </code>
  22. *
  23. * <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.
  24. *
  25. * @author Jack Doyle, jack@greensock.com
  26. */
  27. public class EndArrayPlugin extends TweenPlugin {
  28. /** @private **/
  29. public static const API:Number = 1.0; //If the API/Framework for plugins changes in the future, this number helps determine compatibility
  30. /** @private **/
  31. protected var _a:Array;
  32. /** @private **/
  33. protected var _info:Array = [];
  34. /** @private **/
  35. public function EndArrayPlugin() {
  36. super();
  37. this.propName = "endArray"; //name of the special property that the plugin should intercept/manage
  38. this.overwriteProps = ["endArray"];
  39. }
  40. /** @private **/
  41. override public function onInitTween(target:Object, value:*, tween:TweenLite):Boolean {
  42. if (!(target is Array) || !(value is Array)) {
  43. return false;
  44. }
  45. init(target as Array, value);
  46. return true;
  47. }
  48. /** @private **/
  49. public function init(start:Array, end:Array):void {
  50. _a = start;
  51. var i:int = end.length;
  52. while (i--) {
  53. if (start[i] != end[i] && start[i] != null) {
  54. _info[_info.length] = new ArrayTweenInfo(i, _a[i], end[i] - _a[i]);
  55. }
  56. }
  57. }
  58. /** @private **/
  59. override public function set changeFactor(n:Number):void {
  60. var i:int = _info.length, ti:ArrayTweenInfo;
  61. if (this.round) {
  62. var val:Number;
  63. while (i--) {
  64. ti = _info[i];
  65. val = ti.start + (ti.change * n);
  66. if (val > 0) {
  67. _a[ti.index] = (val + 0.5) >> 0; //4 times as fast as Math.round()
  68. } else {
  69. _a[ti.index] = (val - 0.5) >> 0;
  70. }
  71. }
  72. } else {
  73. while (i--) {
  74. ti = _info[i];
  75. _a[ti.index] = ti.start + (ti.change * n);
  76. }
  77. }
  78. }
  79. }
  80. }
  81. internal class ArrayTweenInfo {
  82. public var index:uint;
  83. public var start:Number;
  84. public var change:Number;
  85. public function ArrayTweenInfo(index:uint, start:Number, change:Number) {
  86. this.index = index;
  87. this.start = start;
  88. this.change = change;
  89. }
  90. }