Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Acceleration with Actionscript 3.0

This is a discussion on Acceleration with Actionscript 3.0 within the Tutorials forums, part of the Flash English category; zzzzzzzzUP' We've previously seen in the article Inertia with Actionscript 3.0 how to create a slowing down motion ...


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 08-10-07, 16:29
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Acceleration with Actionscript 3.0

zzzzzzzzUP'
We've previously seen in the article Inertia with Actionscript 3.0 how to create a slowing down motion effect.
Now I'd like to show how to obtain an accelerating moving effect, up to an established speed point.
Here is how to do it'
I create an FLA and save it as 'accelerazione.fla' .
I create the Document Class, an AS file saved as ' Accelerazione.as ', implemented like so:
Code:
package 
{
	import flash.display.Sprite;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
    
	public class Accelerazione extends Sprite 
	{
		private var sprite:Sprite;
		private var arrivo:int=520;
		private var accelerazioneX:Number=.4;
		private var velocitaX:Number=0;
        
		public function Accelerazione() 
		{
			stage.frameRate=31;
			
			disegnaSprite();
            initSpriteListener();
			initButtonListener();
		}
		
		private function disegnaSprite():void
		{
			sprite=new Sprite();
			sprite.graphics.beginFill(0x666666,100);
			sprite.graphics.drawCircle(0,0,25);
			sprite.graphics.endFill();
			sprite.x=50;
			sprite.y=100;
			addChild(sprite);
		}
		
		private function initSpriteListener():void
		{
			sprite.addEventListener(Event.ENTER_FRAME,muoviSprite);
		}
		private function initButtonListener():void
		{
			riprova_btn.addEventListener(MouseEvent.MOUSE_DOWN,rifai);
		}
        
		private function muoviSprite(e:Event):void 
		{
			velocitaX+=accelerazioneX;
			sprite.x+=velocitaX;
			if(sprite.x>=arrivo)
			{
				sprite.x=arrivo;
				sprite.removeEventListener(Event.ENTER_FRAME,muoviSprite);
			}
		}
		
		private function rifai(m:MouseEvent):void
		{
			sprite.removeEventListener(Event.ENTER_FRAME,muoviSprite);
			sprite.x=50;
			sprite.y=100;
			velocitaX=0;
			initSpriteListener();
		}
	}    
}
Analysing the above code, you can notice a accelerazione and a velocita (speed) variables.
The accelerazione variable is a constant, while the velocita variable starts from zero, is increased at scheduled intervals by the value of accelerazione, and then fed to the x of the sprite.
The code seems full bodied as I've also used a button to allow for a reply.

Here is the result:







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:54..
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
Angular Acceleration and thrust Flep Tutorials 0 02-10-08 04:59


All times are GMT. The time now is 15:46.

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