Forum Topic: AS3: Springs

(1,108 views • 5 replies)

This topic is 1 page long.

<< < > >>
None

trig1

Reply To Post Reply & Quote

Posted at: 5/3/07 12:13 PM

trig1 NEUTRAL LEVEL 15

Sign-Up: 10/04/05

Posts: 2,108

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.

BBS Signature

None

trig1

Reply To Post Reply & Quote

Posted at: 5/3/07 12:17 PM

trig1 NEUTRAL LEVEL 15

Sign-Up: 10/04/05

Posts: 2,108

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

BBS Signature

None

crushy

Reply To Post Reply & Quote

Posted at: 5/3/07 12:19 PM

crushy LIGHT LEVEL 15

Sign-Up: 09/17/05

Posts: 1,842

Cool tut :)

What would we do without the elasticity effect?!

Learning AS3 :D
Help me fix Flash :(

BBS Signature

None

dELtaluca

Reply To Post Reply & Quote

Posted at: 5/3/07 12:46 PM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,548

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.

My social worker says im special!

BBS Signature

None

Monkey-Boy

Reply To Post Reply & Quote

Posted at: 5/3/07 01:33 PM

Monkey-Boy LIGHT LEVEL 15

Sign-Up: 02/12/07

Posts: 2,548

Like elasticity?

BBS Signature

None

I-smel

Reply To Post Reply & Quote

Posted at: 5/3/07 01:46 PM

I-smel NEUTRAL LEVEL 01

Sign-Up: 03/02/06

Posts: 1,260

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


All times are Eastern Standard Time (GMT -5) | Current Time: 10:22 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!