thanks
This is a discussion on Animations with flash CS3 - spring and friction + mouse within the Tutorials forums, part of the Flash English category; Here we are with another article reguarding spring+friction. For the ones who has not read: inertia , acceleration , spring ...
Here we are with another article reguarding spring+friction.
For the ones who has not read: inertia, acceleration , spring and spring and friction, I suggest to give them a look to understand what I will talk about next.
This time I will apply the effect based on the coordinates of the user's mouse.
I would like to point to the fact that the applied effect is very simple and not very exciting. As always, this article would like to be a tutorial before an animation and so, I let space to your fantasy to apply other solutions to obtain animations up to your applications.
Let's get into it...
I create a FLA and save it as 'spring_2.fla', into which I create a MovieClip (of whatever shape) and to which I assign the instance name 'square_mc'.
I create a Document Class, an AS file saved as 'Sprin2.as', implemented the following way:
The result:Code:import flash.display.MovieClip; import flash.events.Event; public class Spring2 extends MovieClip { private const spring:Number=.1; private const frizione:Number=.9; private var vel_x:Number=0; private var vel_y:Number=0; public function Spring2() { init(); } private function init():void { stage.frameRate=31; square_mc.addEventListener(Event.ENTER_FRAME,muovi); } private function muovi(e:Event):void { var acc_x:Number=(mouseX-square_mc.x)*spring; var acc_y:Number=(mouseY-square_mc.y)*spring; vel_x+=acc_x; vel_y+=acc_y; vel_x*=frizione; vel_y*=frizione; square_mc.x+=vel_x; square_mc.y+=vel_y; } } }
Let's analise the code.
Properties
a numerical constant 'spring'
private const spring:Number=.1;
a numerical constant 'frizione'
private const frizione:Number=.9;
the two speeds which will be modified by the spring and friction before being passed to X and Y
private var vel_x:Number=0;
private var vel_y:Number=0;
Methods
init();
I impost the frame rate
stage.frameRate=31;
I add an interval ENTER_FRAME which calls the function muovi() as many times by seconds as the value of the frame rate
square_mc.addEventListener(Event.ENTER_FRAME,muovi );
muovi();
I create a variable which contains the value of the inertia effect applied to the mouse coordinates and I add the spring effect
var acc_x:Number=(mouseX-square_mc.x)*spring;
var acc_y:Number=(mouseY-square_mc.y)*spring;
I add to vel_x and vel_y ( at each interval's iteration) the inertia
vel_x+=acc_x;
vel_y+=acc_y;
I apply the friction to vel_x and vel_y
vel_x*=frizione;
vel_y*=frizione;
I pass vel_x and vel_y to the x and y of 'square_mc'
square_mc.x+=vel_x;
square_mc.y+=vel_y;
Stay tuned !
thanks
Bookmarks