+ Reply to Thread
Results 1 to 2 of 2

Animations with flash CS3 - spring and friction + mouse

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

  1. #1
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,762
    Rep Power
    11

    Animations with flash CS3 - spring and friction + mouse

    Here we are with another article reguarding spring+friction.
    For the ones who has not read: inertiaacceleration , 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:
    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;
    		}
    	}
    }
    The result:







    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 !

  2. #2
    Junior Member Settled In SwalifNet is on a distinguished road
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Re: Animations with flash CS3 - spring and friction + mouse

    thanks

+ Reply to Thread

LinkBacks (?)


Similar Threads

  1. Animazioni con Flash CS3 - spring e frizione + mouse
    By Flep in forum Articoli e tutorials
    Replies: 2
    Last Post: 02-08-09, 22:25
  2. Replies: 2
    Last Post: 14-11-08, 11:09
  3. Replies: 2
    Last Post: 24-03-08, 07:53
  4. Replies: 0
    Last Post: 03-10-07, 18:38
  5. Replies: 0
    Last Post: 27-09-07, 08:20

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts