Posted By

orazal on 01/26/10


Tagged

resize size tween position as2 reposition


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

orazal


Multiple resize & reposition


 / Published in: ActionScript
 

When user clicks one one item it makes that item bigger while adjusting the other items size & position without each item having to check it's neighbors position onEnterFrame. Code still in need of refactoring.

  1. stop();
  2. import flash.geom.Matrix;
  3. import com.orazal.oralib2.transitions.MultiTweener;
  4. import com.robertpenner.easing.*;
  5.  
  6.  
  7. var totalItems:Number = 5;
  8. var itemWidth:Number = 330;
  9. var totalWidth:Number = 875;
  10. var thumbSpacing:Number = 2;
  11. var adjustedWidth:Number = totalWidth - (thumbSpacing * (totalItems-1));
  12. var thumbWidth:Number = adjustedWidth /totalItems;
  13. var smallThumbWidth:Number = (adjustedWidth-itemWidth)/4;
  14. var itemList:Array = [item0, item1, item2, item3, item4];
  15. var activeItem:MovieClip = null;
  16.  
  17. // Init
  18. placeThumbs();
  19.  
  20. function placeThumbs(){
  21.  
  22. // Go through itemList
  23. for(var i:Number=0; i<itemList.length; i++)
  24. {
  25. var item:MovieClip = itemList[i];
  26.  
  27. // Original size & position
  28. item.originalMatrix = item.transform.matrix;
  29. item.originalMatrix.tx = (smallThumbWidth*i) + (thumbSpacing*i);
  30.  
  31. // Small thumb size
  32. item.smallThumbMatrix = item.transform.matrix;
  33. item.smallThumbMatrix.a = item.smallThumbMatrix.d = smallThumbWidth/itemWidth;
  34.  
  35. // Thumb size & position
  36. item.thumbMatrix = item.transform.matrix;
  37. item.thumbMatrix.a = item.thumbMatrix.d = thumbWidth/itemWidth;
  38. item.thumbMatrix.tx = (thumbWidth*i) + (thumbSpacing*i);
  39. item.transform.matrix = item.thumbMatrix;
  40.  
  41. item.onRelease = function()
  42. {
  43. itemReleaseHandler(this);
  44. }
  45. }
  46. }
  47.  
  48. function itemReleaseHandler(item:MovieClip)
  49. {
  50. // If it's already selected
  51. if(activeItem == item)
  52. {
  53. activeItem = null;
  54. // close all
  55. for(var i:Number=0; i<itemList.length; i++)
  56. {
  57. var clip:MovieClip = itemList[i];
  58. tweenToThumb(clip);
  59. }
  60. return;
  61. }
  62.  
  63. // If none or another one is selected
  64. if(activeItem != this)
  65. {
  66. activeItem = item;
  67. tweenToOriginal(item);
  68.  
  69. var position:Number = Number(item._name.substr(4));
  70. for(var i:Number=0; i<itemList.length; i++)
  71. {
  72. var clip:MovieClip = itemList[i];
  73. // Left of active
  74. if(i < position)
  75. {
  76. clip.smallThumbMatrix.tx = (smallThumbWidth*i) + (thumbSpacing*i);
  77. tweenToSmallThumb(clip);
  78. }
  79. // Right of active
  80. if(i > position)
  81. {
  82. clip.smallThumbMatrix.tx = (smallThumbWidth*(i-1)) + (thumbSpacing*i);
  83. clip.smallThumbMatrix.tx += itemWidth;
  84. tweenToSmallThumb(clip);
  85. }
  86. }
  87. return;
  88. }
  89.  
  90. }
  91.  
  92. function tweenToOriginal(item:MovieClip)
  93. {
  94. item.tween.stop();
  95.  
  96. var startMatrix:Matrix = item.transform.matrix;
  97. var endMatrix:Matrix = item.originalMatrix;
  98. item.tween = new MultiTweener(startMatrix, endMatrix,
  99. Circ.easeOut, 0.5, true);
  100. item.tween.onMotionChanged = function(mt:MultiTweener) {
  101. item.transform.matrix = startMatrix;
  102. }
  103. }
  104.  
  105. function tweenToThumb(item:MovieClip)
  106. {
  107. item.tween.stop();
  108. var startMatrix:Matrix = item.transform.matrix;
  109. var endMatrix:Matrix = item.thumbMatrix;
  110. item.tween = new MultiTweener(startMatrix, endMatrix,
  111. Circ.easeOut, 0.5, true);
  112. item.tween.onMotionChanged = function(mt:MultiTweener) {
  113. item.transform.matrix = startMatrix;
  114. }
  115. }
  116.  
  117. function tweenToSmallThumb(item:MovieClip)
  118. {
  119. item.tween.stop();
  120. var startMatrix:Matrix = item.transform.matrix;
  121. var endMatrix:Matrix = item.smallThumbMatrix;
  122. item.tween = new MultiTweener(startMatrix, endMatrix,
  123. Circ.easeOut, 0.5, true);
  124. item.tween.onMotionChanged = function(mt:MultiTweener) {
  125. item.transform.matrix = startMatrix;
  126. }
  127. }

Report this snippet  

You need to login to post a comment.