Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Animations with Flash CS3 - spring on 3 points

This is a discussion on Animations with Flash CS3 - spring on 3 points within the Tutorials forums, part of the Flash English category; Here we are once again on how to use the spring animations ( see article: Animations with Flash CS3 - spring and ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 27-09-07, 09:20
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Animations with Flash CS3 - spring on 3 points

Here we are once again on how to use the spring animations ( see article: Animations with Flash CS3 - spring and friction ).
This time, I use the spring effect on a MovieClip moved from 3 points and so, the movement will be dominated by the 3 different forces.
Clearly, giving space to your imagination, animations can be created to capture the user's interest letting him interact a little with the application.
I leave it to you to develope the example further.

Let's see the tutorial...

I create a FLA and save it as 'spring_3.fla' into which:
- I create 3 square MovieClips with instance names 'punto_0_mc', 'punto_1_mc' and 'punto_2_mc'.
- I create a circolar MovieClip with an instance name 'ball_mc'

I create a Document Class, an AS file saved as 'Spring3.as', implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.Event;
	
	public class Spring3 extends MovieClip
	{
		private var clips_array:Array;
		
		private const spring:Number=.1;
		private const frizione:Number=.9;
		private var vx:Number=0;
		private var vy:Number=0;

		public function Spring3()
		{
			init();
		}
		
		private function init():void
		{
			stage.frameRate=31;
			
			clips_array=new Array(punto_0_mc,punto_1_mc,punto_2_mc);
			
			addListeners();
		}
		
		private function addListeners():void
		{
			for(var i:int=0;i < clips_array.length;i++)
			{
				clips_array[i].buttonMode=true;
				clips_array[i].addEventListener(MouseEvent.MOUSE_DOWN,doDrag);
				clips_array[i].addEventListener(MouseEvent.MOUSE_UP,dropDrag);
			}
			
			this.addEventListener(Event.ENTER_FRAME,muoviDisegna);
		}
		
		private function doDrag(m:MouseEvent):void
		{
			m.target.startDrag();
		}
		
		private function dropDrag(m:MouseEvent):void
		{
			m.target.stopDrag();
		}
		
		private function muoviDisegna(e:Event):void
		{
			this.graphics.clear();
			this.graphics.lineStyle(1,0x333333,1);
			
			for(var i:int=0;i < clips_array.length;i++)
			{
				var ax:Number=(clips_array[i].x-ball_mc.x)*spring;
				var ay:Number=(clips_array[i].y-ball_mc.y)*spring;
				vx+=ax;
				vy+=ay;
				
				this.graphics.moveTo(clips_array[i].x,clips_array[i].y);
				this.graphics.lineTo(ball_mc.x,ball_mc.y);
			}
			vx*=frizione;
			vy*=frizione;
			ball_mc.x+=vx;
			ball_mc.y+=vy;
		}
	}
}
The result:







Let's analise the code.

Properties

an array in which I insert 3 square movieclips
private var clips_array:Array;
the famous numerical constant 'spring'
private const spring:Number=.1;
an another one is the numerical constant 'frizione'
private const frizione:Number=.9;
two numerical variables which will be the speed of ball_mc
private var vx:Number=0;
private var vy:Number=0;

Methods
init();
I impost the frame rate
stage.frameRate=31;
I initialize an array into which I insert the square movieclips
clips_array=new Array(punto_0_mc,punto_1_mc,punto_2_mc);
I call the addListeners method in which I will add the needed listeners
addListeners();

addListeners();
I create a cycle with the length array as a maximum iteration
for(var i:int=0;i < clips_array.length;i++)
{
for each clip I add the button mode so that it can be clicked with the mouse during the events
clips_array[i].buttonMode=true;
for each clip I add a listener to the event MOUSE_DOWN which will call the doDrag() method
clips_array[i].addEventListener(MouseEvent.MOUSE_DOWN,doDrag);
for each clip I add a listener to the event MOUSE_UP which will call the dropDrag() method
clips_array[i].addEventListener(MouseEvent.MOUSE_UP,dropDrag);
}
I add a listener (to the ex_root) to the event ENTER_FRAME which will call the muoviDisegna() method tot times by seconds based on the frame rate
this.addEventListener(Event.ENTER_FRAME,muoviDiseg na);

doDrag(m:MouseEvent)
I tell the clip to start the drag
m.target.startDrag();

dropDrag(m:MouseEvent);
I tell the clip to stop the drag
m.target.stopDrag();

muoviDisegna();
let's remember that the 'this' in this case refers to the TimeLine
I clear the graphics
this.graphics.clear();
I initialize the graphics
this.graphics.lineStyle(1,0x333333,1);
I create a cycle with the array 'clips_array' lenght as a maximum iteration
for(var i:int=0;i < clips_array.length;i++)
{
I apply the inertia to the clip's array X and Y. At this moment, the inertia of the 3 array's MovieClip interact together on a unique X and Y speed (variables vx and vy)
var ax:Number=(clips_array[i].x-ball_mc.x)*spring;
var ay:Number=(clips_array[i].y-ball_mc.y)*spring;
vx+=ax;
vy+=ay;
I design the lines in between the clips and ball_mc
this.graphics.moveTo(clips_array[i].x,clips_array[i].y);
this.graphics.lineTo(ball_mc.x,ball_mc.y);
}
I apply the friction to ball_mc
vx*=frizione;
vy*=frizione;
ball_mc.x+=vx;
ball_mc.y+=vy;

See you soon !
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Cue points and Flash CS3 Flep Tutorials 7 05-11-08 08:10
Animations with flash CS3 - spring and friction + mouse Flep Tutorials 1 15-10-08 12:44
Cue points e Flash CS3 Flep Articoli e tutorials 4 27-09-08 11:50
Animations with Actionscript 3.0 - spring and friction Flep Tutorials 2 24-03-08 08:53
Animations with Actionscript 3.0 - the spring Flep Tutorials 0 03-10-07 19:38


All times are GMT. The time now is 19:19.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap