Posted By

onemanstudio on 05/18/11


Tagged

path distance track travel bezier trails trail traveled


Versions (?)

AS3 Trails


 / Published in: ActionScript 3
 

URL: http://onemanstudio.eu/labs/rabbitTrails

AS3 Trails, just call checkDistance() during Your Tween

  1. package eu.onemanstudio
  2. {
  3. import flash.display.MovieClip;
  4. import flash.display.StageAlign;
  5. import flash.events.Event;
  6. import com.greensock.*;
  7. import com.greensock.easing.*;
  8. import eu.onemanstudio.Arrow;
  9.  
  10.  
  11. /**
  12. * ...
  13. * @author [email protected]
  14. */
  15. public class Main extends MovieClip
  16. {
  17. private var countX:Number = 0;
  18. private var lastX:Number = 0;
  19. private var currentX:Number = 0;
  20. private var differenceX:Number = 0;
  21. private var countY:Number = 0;
  22. private var lastY:Number = 0;
  23. private var currentY:Number = 0;
  24. private var differenceY:Number = 0;
  25.  
  26. private var howMany:int = 20; // Trail every 20 pixels traveled
  27.  
  28. private var eMCe:Arrow; //Trail MovieClip
  29. private var mc:Arrow; //Traveler
  30.  
  31. public function Main():void
  32. {
  33. if (stage) init();
  34. else addEventListener(Event.ADDED_TO_STAGE, init);
  35. }
  36.  
  37. private function init(e:Event = null):void
  38. {
  39. removeEventListener(Event.ADDED_TO_STAGE, init);
  40. // entry point
  41. stage.align = StageAlign.TOP_LEFT;
  42.  
  43. mc = new Arrow();
  44. addChild(mc);
  45. mc.x = 600;
  46. mc.y = 30;
  47.  
  48. TweenMax.to(mc, 15, {bezierThrough:[{x:60, y:183}, {x:400, y:440}, {x:320, y:570}, {x:520, y:70}], delay:2, orientToBezier:true, onUpdate:checkDistance});
  49. }
  50.  
  51.  
  52.  
  53.  
  54. private function checkDistance():void
  55. {
  56. currentX = mc.x;
  57. if(lastX && lastX != 0)
  58. {
  59. differenceX = lastX - currentX;
  60. if (differenceX <= 0) differenceX = differenceX * -1;
  61. }
  62. lastX = currentX;
  63. if (differenceX != 0) countX += Number(differenceX);
  64.  
  65. if (countX >= howMany) trail();
  66.  
  67. currentY = mc.y;
  68. if(lastY && lastY != 0)
  69. {
  70. differenceY = lastY - currentY;
  71. if(differenceY <=0) differenceY = differenceY * -1;
  72. }
  73. lastY = currentY;
  74. if (differenceY != 0) countY += Number(differenceY);
  75.  
  76. if (countY >= howMany) trail();
  77. }
  78.  
  79. private function trail():void
  80. {
  81. eMCe = new Arrow();
  82. addChild(eMCe);
  83. eMCe.x = mc.x;
  84. eMCe.y = mc.y;
  85. eMCe.scaleX = eMCe.scaleY = 0.2;
  86. eMCe.rotation = mc.rotation;
  87. countY = 0;
  88. countX = 0;
  89.  
  90. }
  91.  
  92.  
  93.  
  94. }
  95.  
  96. }

Report this snippet  

You need to login to post a comment.