Posted By

dx0ne on 11/27/09


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

djmaddy


Walk on Bezier


 / Published in: ActionScript 3
 

URL: http://www.reflektions.com/miniml/template_permalink.asp?id=266

  1. // Initial variables
  2. var numPoints = 5; // number of points
  3. var maxSpeed = .02; // travel speed
  4. var focalLength = 500; // environmental constant
  5. var centerX = Stage.width/2; // Stage Center X
  6. var centerY = Stage.height/2; // Stage Center Y
  7.  
  8. //Initiate Animation
  9. this.initFollowPath = function(){
  10. // create data object to store points coordinates
  11. oData = new Object();
  12. // create camera object
  13. this.oCamera = new Object();
  14. // Set camera Properties
  15. this.oCamera.z = focalLength; // target camera z position
  16. this.oCamera.dz = 0; // initial camera z position
  17. this.oCamera.s = maxSpeed; // camera zoom speed
  18.  
  19. // associate button action
  20. for(var i = 0; i<numPoints;i++){
  21. var d = -(i+1);
  22. // attach point
  23. var anchor = attachMovie("anchor", "point_"+ i, d);
  24. // attach associated control point
  25. var control = attachMovie("control", "ctrl_"+ i,100*d);
  26. // set random position
  27. anchor._x = control._x = random(Stage.width);
  28. anchor._y = control._y = random(Stage.height);
  29. // set buttons
  30. anchor.onPress = this.initDrag;
  31. anchor.onRelease = point.onReleaseOutside= this.endDrag;
  32. control.onPress = this.initDrag;
  33. control.onRelease = control.onReleaseOutside= this.endDrag;
  34. }
  35.  
  36. // create follower
  37. var clip = this.attachMovie("follow_mc","follow_mc", 1);
  38. // set properties
  39. clip.t = 0; // start travel time
  40. clip.num = 0; // start at this point
  41. clip.speed = maxSpeed; // set speed
  42. // render clip
  43. clip.onEnterFrame = render;
  44.  
  45. // create a clip for the line
  46. this.createEmptyMovieClip("line_mc",10);
  47. // start rendering line
  48. this.onEnterFrame = drawLine;
  49.  
  50. }
  51.  
  52. // render
  53. this.render = function () {
  54.  
  55. //increment time
  56. this.t += slider/1000; // value from scrolBar
  57. // add num if it reaches 1 if it reaches last point set back to 0
  58. this.num = ((this.num + (this.t >= 1)) * (this.num<numPoints));
  59. // reset to to 0 if we reach 1
  60. this.t%=1;
  61.  
  62. // set init//end and controler clip position for animation
  63. // according to the value of num
  64. for(var i = 0; i<=numPoints;i++){
  65. if (this.num == i){
  66. var initX = oData["cx"+i];
  67. var initY = oData["cy"+i];
  68. // keep looping by reseting to 0 if we reach last point
  69. var bezX = (i==numPoints-1) ? oData.cx0 : oData["cx"+(i+1)];
  70. var bezY = (i==numPoints-1) ? oData.cy0 : oData["cy"+(i+1)];
  71. // keep looping by reseting to 0 if we reach last point
  72. var endX = (i==numPoints-1) ? oData.x0: oData["x"+(i+1)];
  73. var endY = (i==numPoints-1) ? oData.y0: oData["y"+(i+1)];
  74. }
  75. }
  76.  
  77. // keep old positions in memory
  78. var dx = this._x;
  79. var dy = this._y;
  80.  
  81. // set position to follow_mc
  82. this._x = setBezierPos(initX,endX,bezX,this.t);
  83. this._y = setBezierPos(initY,endY,bezY,this.t);
  84.  
  85. // difference in position
  86. dx = this._x-dx;
  87. dy = this._y-dy;
  88. // roate clip
  89. this._rotation = Math.atan2(dy, dx)*180/Math.PI;
  90.  
  91. }
  92.  
  93. // Set bezier curve to follow
  94. this.setBezierPos = function(p1,p2,p3,t){
  95. // Equation to calculat position in curve
  96. return p1*(1-t)*(1-t)+2*p2*(1-t)*t+p3*t*t;
  97. }

Report this snippet  

You need to login to post a comment.