/ Published in: ActionScript 3
URL: http://www.reflektions.com/miniml/template_permalink.asp?id=266
Expand |
Embed | Plain Text
// Initial variables var numPoints = 5; // number of points var maxSpeed = .02; // travel speed var focalLength = 500; // environmental constant var centerX = Stage.width/2; // Stage Center X var centerY = Stage.height/2; // Stage Center Y //Initiate Animation this.initFollowPath = function(){ // create data object to store points coordinates oData = new Object(); // create camera object this.oCamera = new Object(); // Set camera Properties this.oCamera.z = focalLength; // target camera z position this.oCamera.dz = 0; // initial camera z position this.oCamera.s = maxSpeed; // camera zoom speed // associate button action for(var i = 0; i<numPoints;i++){ var d = -(i+1); // attach point var anchor = attachMovie("anchor", "point_"+ i, d); // attach associated control point var control = attachMovie("control", "ctrl_"+ i,100*d); // set random position anchor._x = control._x = random(Stage.width); anchor._y = control._y = random(Stage.height); // set buttons anchor.onPress = this.initDrag; anchor.onRelease = point.onReleaseOutside= this.endDrag; control.onPress = this.initDrag; control.onRelease = control.onReleaseOutside= this.endDrag; } // create follower var clip = this.attachMovie("follow_mc","follow_mc", 1); // set properties clip.t = 0; // start travel time clip.num = 0; // start at this point clip.speed = maxSpeed; // set speed // render clip clip.onEnterFrame = render; // create a clip for the line this.createEmptyMovieClip("line_mc",10); // start rendering line this.onEnterFrame = drawLine; } // render this.render = function () { //increment time this.t += slider/1000; // value from scrolBar // add num if it reaches 1 if it reaches last point set back to 0 this.num = ((this.num + (this.t >= 1)) * (this.num<numPoints)); // reset to to 0 if we reach 1 this.t%=1; // set init//end and controler clip position for animation // according to the value of num for(var i = 0; i<=numPoints;i++){ if (this.num == i){ var initX = oData["cx"+i]; var initY = oData["cy"+i]; // keep looping by reseting to 0 if we reach last point var bezX = (i==numPoints-1) ? oData.cx0 : oData["cx"+(i+1)]; var bezY = (i==numPoints-1) ? oData.cy0 : oData["cy"+(i+1)]; // keep looping by reseting to 0 if we reach last point var endX = (i==numPoints-1) ? oData.x0: oData["x"+(i+1)]; var endY = (i==numPoints-1) ? oData.y0: oData["y"+(i+1)]; } } // keep old positions in memory var dx = this._x; var dy = this._y; // set position to follow_mc this._x = setBezierPos(initX,endX,bezX,this.t); this._y = setBezierPos(initY,endY,bezY,this.t); // difference in position dx = this._x-dx; dy = this._y-dy; // roate clip this._rotation = Math.atan2(dy, dx)*180/Math.PI; } // Set bezier curve to follow this.setBezierPos = function(p1,p2,p3,t){ // Equation to calculat position in curve return p1*(1-t)*(1-t)+2*p2*(1-t)*t+p3*t*t; }
You need to login to post a comment.
