Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Animazioni con Flash CS3 - the spring

This is a discussion on Animazioni con Flash CS3 - the spring within the Articoli e tutorials forums, part of the Flash CS3 e Actionscript 3.0 category; Dopo aver visto come creare un movimento inerziale e successivamente imparato ad applicare un effetto di accelerazione con Actionscript 3....


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 e Actionscript 3.0 > Articoli e tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 19-09-07, 15:58
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,486
Rep Power: 6
Flep is on a distinguished road
Animazioni con Flash CS3 - the spring


Dopo aver visto come creare un movimento inerziale e successivamente imparato ad applicare un effetto di accelerazione con Actionscript 3.0, i tempi sono maturi per iniziare la cosìdetta ' spring ' .

La spring deriva dall' inglese ed è un effetto tipo molla.



Vediamo come creare questa utile animazione...


La Document Class che ho scritto è la seguente:
Code:
package
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class Spring_0 extends Sprite
	{
		private var sprite:Sprite;
		
		private var spring:Number=.1;
		private var center:Number;
		private var vel_x:Number=0;
		
		public function Spring_0()
		{
			drawSprite();
			initListener();
		}
		
		private function drawSprite():void
		{
			sprite=new Sprite();
			sprite.graphics.beginFill(0x0066FF,1);
			sprite.graphics.drawCircle(20,stage.stageHeight/2,20);
			addChild(sprite);
			
			sprite.x=vel_x;
			
			center=stage.stageWidth/2-sprite.width/2;
		}
		
		private function initListener():void
		{
			sprite.addEventListener(Event.ENTER_FRAME,muoviSprite);
		}
		
		private function muoviSprite(e:Event):void
		{
			var acc_x:Number=(center-sprite.x)*spring;
			vel_x+=acc_x;
			sprite.x+=vel_x;
			//sprite.scaleX=sprite.scaleY=Math.abs(vel_x/100);
		}
	}
}
E questo è l'effetto ottenuto:

Risultato:











Se analizziamo il codice, notiamo che non c'è molto di più in confronto ad un effetto inerzia o accelerazione.

Il metodo drawSprite disegna la sprite.

Il metodo initListener aggiunge un listener in ascolto su ENTER_FRAME

Questa riga:

var ine_x:Number=(center-sprite.x)*spring;

non è altro che la formula dell' inerzia che abbiamo applicato in questo articolo.

Poi, invece di dare ine_x subito in pasto alla X della sprite, lo diamo in pasto al valore di vel_x

vel_x+=ine_x;

ed infine diamo vel_x in pasto alla X della sprite:

sprite.x+=vel_x;



NB: adesso si potrebbe ' giocare ' anche con le altre proprietà della sprite tipo scaleX e scaleY. Aggiungendo ad esempio questa riga:

sprite.scaleX=sprite.scaleY=Math.abs(vel_x/100);

otteniamo questo effetto:










Buon divertimento !
__________________

 


I recommend: Essential Actionscript 3.0

- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
- I do not reply technicians pvt messages. Open a thread !
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
Animazioni con Flash CS3 - spring frizione e gravità Flep Articoli e tutorials 3 03-07-08 17:11
Animazioni con Flash CS3 - spring e frizione Flep Articoli e tutorials 2 27-04-08 23:50
Animazioni con Flash CS3 - spring su 3 punti Flep Articoli e tutorials 0 20-09-07 14:24
Animazioni con Flash CS3 - spring e frizione + mouse Flep Articoli e tutorials 0 20-09-07 13:22
Animazioni con Flash CS3 - spring frizione e gravità zakwer Actionscript 3.0 avanzato 1 10-09-07 10:12


All times are GMT. The time now is 11:52.


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


FlepStudio
by Filippo Lughi
P.IVA 03605860406