00:00
00:00

AS3: Springs 2007-05-03 12:13:08


AS3: Main

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 xdif:Number;
var ydif:Number;
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.addEventListener(Event.ENTER_FRAME, OEF);
function OEF(Event){

/* 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.

Response to AS3: Springs 2007-05-03 12:17:24


At 5/3/07 12:13 PM, trig1 wrote: AS3: Main

FUUUUUCK!!!! Sorry, the first thing on this is wrong. It links to the "Happy Pico Day" thread, not AS3: Main.

If any mod/admin sees this, is there any chance you could edit my original post (if mods/admins can do that) so it works? Thanks :D

Response to AS3: Springs 2007-05-03 12:19:44


Cool tut :)

What would we do without the elasticity effect?!


BBS Signature

Response to AS3: Springs 2007-05-03 12:46:15


sorry, but this shouldnt be called springs. i thought it would ATLEAST deal with spring force and ATLEAST spring forces when dealing with non zero length springs, and spring dampening etc.


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature

Response to AS3: Springs 2007-05-03 13:33:47


Like elasticity?


BBS Signature

Response to AS3: Springs 2007-05-03 13:46:47


BUT can you add gravity to that? So it's like the square is attached to the mouse.