AS3: Springs 2007-05-03 12:13:08
The end result of this tutorial should hopefully be something like this.
The idea behind it - The code works out the difference between the movieclip and the mouse, and then adds a proportion of this onto the speed of the movieclip.
How to make it - Create a movieclip, and give it an instance name of "box". Then put this code on the main timeline.
var xvel:Number = 0;
var yvel:Number = 0;
/*So we have x and y vel (vel is shortened from velocity), which at the moment is 0, and x and y dif, which are currently undefined. These will contain the speed of the movieclip, and the difference between the mouse and the movieclips co-ordinates.*/
/* This creates a function called OEF that is called when the event ENTER_FRAME happens*/
xdif = mouseX - box.x;
ydif = mouseY - box.y;
/* This sets the variables ydif and xdif as the difference between the the x/y coordinates. */
xvel += xdif*0.4;
yvel += ydif*0.4;
/* This increases xvel and yvel by a proportion of xdif and ydif. You can change 0.4 to anything between 0-1, the higher it is the faster the box goes. */
xvel *= 0.8;
yvel *= 0.8;
/* This decreases the variables xvel and yvel, so that it doesnt bounce on forever. Again, you can change it between 0-1, depending on how bouncy you want it. */
box.x += xvel;
box.y += yvel;
/* This makes the box MC's coordinates increase by the variables xvel and yvel. */
And there you have it, your own spring!
Please give feedback, this is only my second tutorial.