Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Inertia with Actionscript 3.0

This is a discussion on Inertia with Actionscript 3.0 within the Tutorials forums, part of the Flash English category; You'll have often come across animations or effects simulating Physics or Maths laws with Actionscript. What I want to ...


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 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 09-10-07, 19:20
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Inertia with Actionscript 3.0

You'll have often come across animations or effects simulating Physics or Maths laws with Actionscript.
What I want to introduce is a long chapter not always easy to understand.
Watch this space to see some really interesting things!

So, let us begin...

The first effect useful to learn is called ' inertial movement ' .
In a few words it"s about those animations that start fast and increasingly slow down to a stand still point.
Obviously I"m referring to object moving animations with Actionscript, but this effect is also applicable to other properties like the alpha or the rotation, just to cite a couple of examples.

Let"s see the juice:

I create an FLA and save it as " inerzia.fla " then I create the Document Class, an AS file that I save as " Inerzia ".
Here it is:
Code:
package 
{
	import flash.display.Sprite;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class Inerzia extends Sprite
	{
		private var quadrato:Sprite;
		private var arrivo:int=400;
		
		public function Inerzia()
		{
			stage.frameRate=31;
			
			creaSprite();
			initQuadratoListener();
			initBottoneListener();
		}
		
		private function creaSprite():void
		{
			quadrato=new Sprite();
			quadrato.graphics.beginFill(0xFF0066);
			quadrato.graphics.drawRect(100,100,50,50);
			quadrato.graphics.endFill();
			addChild(quadrato);
		}
		
		private function initQuadratoListener():void
		{
			quadrato.addEventListener(Event.ENTER_FRAME,muoviQuadrato);
		}
		
		private function initBottoneListener():void
		{
			riprova_btn.addEventListener(MouseEvent.MOUSE_DOWN,replay);
		}
		
		private function muoviQuadrato(e:Event):void
		{
			var distanza:Number=arrivo-quadrato.x;
			var inerziale:Number=distanza*.1;
			quadrato.x+=inerziale;
			if(Math.abs(distanza)<=.5)
			{
				quadrato.x=arrivo;
				quadrato.removeEventListener(Event.ENTER_FRAME,muoviQuadrato);
			}
		}
		
		private function replay(m:MouseEvent):void
		{
			quadrato.removeEventListener(Event.ENTER_FRAME,muoviQuadrato);
			quadrato.x=0;
			initQuadratoListener();
		}
	}
}
Here is the result:








Note that the class extends the Sprite class and not the MovieClip simply because I reduce the memory and I don"t import the MovieClip class, but only the Sprite one.
If the MovieClip (s) are hand-drawn in the FLA, then the class will have to extend the MovieClip class.
What shall I do:
I create a variable called " arrivo "
I draw a simple square Sprite and I assign a certain X and Y to it.
I add the listeners ( in this case there"s also a listener for the " Riprova " button)
I start an interval ( ENTER_FRAME )

The variable " distanza " is always the difference between the end point ( the value of the variable " arrivo ") and the X of the Sprite.
The variable " inerziale " divides the " distanza " by 10, as * .1 equals to /10
Let"s remember always that we have to interrupt the interval when the Sprite reaches the end point or we"ll always have an open interval, with CPU consequences for the user.
Therefore I stop the interval when the value of the variable " distanza " is less than or equal to 0.5 .

Stay tuned!
__________________

 


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 !

Last edited by Flep; 28-08-08 at 06:35..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)  
Old 14-05-08, 19:16
Junior Member
 
Join Date: May 2008
Posts: 3
Rep Power: 0
gmsi is on a distinguished road
Re: Inertia with Actionscript 3.0

Hi Flep
Thank you for this tutorial, I am new to action script3. I followed the tutorial. After spending time trying... huh still did not work like the sample above
I added the button "riprova_btn" on the stage but not how to create the action on the first frame .
by any chance is there the fla file which i can take a look or what to write the variable on the first frame. Any help I would appreciate.

again thank you for your effort
gmsi
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 14-05-08, 21:07
Junior Member
 
Join Date: May 2008
Posts: 3
Rep Power: 0
gmsi is on a distinguished road
Re: Inertia with Actionscript 3.0

Hi Flep
please ignore my question.
I figured out.
I am new to AC3 and doesn't know about Document Class on the publish setting.
learn something new today
gmsi
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 14-05-08, 21:52
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Re: Inertia with Actionscript 3.0

Hi gmsi,
about document class you can read these:

The Document Class of Flash CS3

Call from the timeline to the Document Class and vice versa with Flash CS3
__________________

 


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
  #5 (permalink)  
Old 14-05-08, 22:01
Junior Member
 
Join Date: May 2008
Posts: 3
Rep Power: 0
gmsi is on a distinguished road
Smile Re: Inertia with Actionscript 3.0

Hi
for the last couple hours I have been here reading tru your tutorials.
I was hesitating to learn Action Script3, you inspired me to put my time and learn AC3.
Yes I take a look at the tutorials you mentioned, it does helps understanding the basic AC# document class.
Thank you for your immediate reply.Sure I will be visiting your site often.

Regards
gmsi
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 14-05-08, 22:02
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Re: Inertia with Actionscript 3.0

You are welcome
__________________

 


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
Multiple inertia with Flash CS3 Flep Tutorials 0 25-09-07 17:55


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

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