Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Trigonometry with Actionscript 3.0 [example3]

This is a discussion on Trigonometry with Actionscript 3.0 [example3] within the Tutorials forums, part of the English Forums category; The series of articles/tutorials on the trigonometry applied to Actionscript 3.0 carry on. We get into the heart ...


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

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 27-09-07, 10:11
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,446
Rep Power: 6
Flep is on a distinguished road
Trigonometry with Actionscript 3.0 [example3]

The series of articles/tutorials on the trigonometry applied to Actionscript 3.0 carry on.
We get into the heart of this argument using angles, radius, sinus and cosinus.
In this example, we will see how to move a simple ball using trigonometry.
We will see how to create an effect with more balls using the library's MovieClip ID to create more instances of the same MovieClip. Everything under an interval.
At the end, we will see how to apply this effect to a mask.
Let's take a look at the code...

First example:

I create a FLA and save it as 'trigonometria_3.fla'.
Inside it, I create a MovieClip of circular shape with an instance name 'ball_mc'.
I create a Document Class, an AS file saved as 'Trigonometria.as', implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	
	public class Trigonometria extends MovieClip
	{
		private var centroY:Number;
		private var angolo:Number=0;
		private const raggio:Number=150;
		private const velocita:Number=1;

		public function Trigonometria()
		{
			init();
		}
		
		private function init():void
		{
			stage.frameRate=31;
			
			ball_mc.x=0;
			ball_mc.y=stage.stageHeight/2;
			centroY=ball_mc.y;
			
			ball_mc.addEventListener(Event.ENTER_FRAME,muovi);
		}
		
		private function muovi(e:Event):void
		{
			ball_mc.y=centroY+Math.sin(angolo)*raggio;
			ball_mc.x+=velocita;
			angolo+=.1;
			if(ball_mc.x>=stage.stageWidth+ball_mc.width)
				ball_mc.x=0;
		}
	}
}
Let's analyse the code.

Trigonometria.as class

Properties


a numerical variable counting the value of Y from the center of the circumference
private var centroY:Number;
a numerical variable containing the angle's value (then increased)
private var angolo:Number=0;
a constant containing the dimension of the circumference radius
private const raggio:Number=150;
a numerical variable containing the speed of ball_mc X
private const velocita:Number=1;

Methods

init();
I impost the frame rate
stage.frameRate=31;
I position the MovieClip
ball_mc.x=0;
ball_mc.y=stage.stageHeight/2;
I assign a value to the Y placed at the center of the circumference (in this case, equal to the ball_mc Y)
centroY=ball_mc.y;
I add a listener with an interval ENTER_FRAME which calls the function muovi();
ball_mc.addEventListener(Event.ENTER_FRAME,muovi);

muovi();
I assign to the clip Y a value equal to a, the center of the circumference plus il sinus of the variable 'angolo' multiplied by the value of the variable 'raggio'
ball_mc.y=centroY+Math.sin(angolo)*raggio;
I increase the ball_mc X of the value of the variable 'velocita'
ball_mc.x+=velocita;
I increase the value of the variable 'angolo' of 0.1
angolo+=.1;
if the ball_mc X is bigger then the stage width plus its width
if(ball_mc.x>=stage.stageWidth+ball_mc.width)
the ball_mc X equal zero
ball_mc.x=0;

Second example:

I create a FLA and save it as 'trigonometria_3_a.fla'.
Inside it, I create the same MovieClip of circular shape (mc_ball) but I will leave it in library. I assign to it a linkage ID 'Ball', the same name of the class which I will create.
I create a Document Class, an AS file saved as 'Trigonometria2.as', implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	import flash.utils.Timer;
	import flash.events.Event;
	import flash.events.TimerEvent;
	
	public class Trigonometria2 extends MovieClip
	{
		private var timer:Timer;
		
		private const velocitaTimer:int=150;
		
		public function Trigonometria2():void
		{
			init();
		}
		
		private function init():void
		{
			stage.frameRate=31;
			
			startTimer();
		}
		
		private function startTimer():void
		{
			timer=new Timer(velocitaTimer,0);
			timer.addEventListener(TimerEvent.TIMER,attaccaBall);
			timer.start();
		}
		
		private function attaccaBall(t:TimerEvent):void
		{
			var ball:Ball=new Ball();
			addChild(ball);
			ball.muoviti();
		}
	}
}
I create a Ball class, an AS file saved as 'Ball.as' (which is the ID of mc_ball in library), implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	
	public class Ball extends MovieClip
	{
		private var centroY:Number;
		private var angolo:Number=0;
		private var raggio:Number;
		private var velocita:Number;
		
		public function Ball():void
		{
			init();
		}
		
		private function init():void
		{
			x=0;
		}
		
		public function muoviti():void
		{
			y=this.parent.parent.stage.stageHeight/2;
			centroY=y;
			raggio=10+Math.random()*(y/2);
			velocita=.5+Math.random()*4.5;
			scaleX=scaleY=.2+Math.random();
			alpha=.1+Math.random();
			addEventListener(Event.ENTER_FRAME,go);
		}
		
		private function go(e:Event):void
		{
			y=centroY+Math.sin(angolo)*raggio;
			x+=velocita;
			angolo+=.1;
			if(x>this.parent.parent.stage.stageWidth+width+10)
			{
				fermaIntervallo();
				rimuovimi();
			}
		}
		
		private function fermaIntervallo():void
		{
			removeEventListener(Event.ENTER_FRAME,go);
		}
		
		private function rimuovimi():void
		{
			this.parent.removeChild(this);
		}
	}
}
Let's analyse the code.

Trigonometria2.as class

Properties


a timer (instance of the Timer class)
private var timer:Timer;
a constant 'velocitaTimer' with a value equal to the speed of the timer
private const velocitaTimer:int=150;

Methods

init();
I impost the frame rate
stage.frameRate=31;
I call the function startTimer()
startTimer();

startTimer();
I create an instance of 'timer' passing the value of the constant 'velocitaTimer' and zero (infinite)
timer=new Timer(velocitaTimer,0);
I add a listener to 'timer' which will call every tot of hundredth of seconds (equal to the value of the constant 'velocitaTimer') the function attaccaBall()
timer.addEventListener(TimerEvent.TIMER,attaccaBal l);
I start the timer
timer.start();

attaccaBall();
I create an instance of the Ball class (which we will see next)
var ball:Ball=new Ball();
as the Ball class extends the MovieClip class, I add the instance of the Ball class (ball) to the DisplayObject (otherwise it would not be visible)...ex attachMovie of ball_mc with a linkage ID 'Ball'
addChild(ball);
I call the muoviti() method of the Ball class
ball.muoviti();

Ball.as class ( let's remember that the codes here is directly referred to the MovieClip which will be attach to the stage)

Properties

a numerical variable counting the value of Y from the center of the circumference
private var centroY:Number;
a numerical variable containing the angle's value (then increased)
private var angolo:Number=0;
a constant containing the dimension of the circumference radius
private const raggio:Number=150;
a numerical variable containing the speed of ball_mc X (the MovieClip attached to the stage)
private const velocita:Number=1;

Methods

init();
I impost x equal to zero
x=0;

muoviti();
I impost the y equal to the value of half the stage height
y=this.parent.parent.stage.stageHeight/2;
I impost the center Y of the circumference (in this case equal to ball's y)
centroY=y;
I impost a random value to the circumference radius
raggio=10+Math.random()*(y/2);
I impost a random value to the x speed 'velocita'
velocita=.5+Math.random()*4.5;
I impost a random value to the scaleX and scaleY properties
scaleX=scaleY=.2+Math.random();
I impost a random value to the alpha property
alpha=.1+Math.random();
I call the function go() within an interval ENTER_FRAME
addEventListener(Event.ENTER_FRAME,go);

go();
I impost a value to y equal to the center Y of the circumference plus the sinus of the value of the variable 'angolo' multiplied by the value of the variable 'raggio'
y=centroY+Math.sin(angolo)*raggio;
I increase the x of the value of the variable 'raggio'
x+=velocita;
I increase the angle of 0.1
angolo+=.1;
now, when the clip will arrive to the point on stage where it will not be visible anymore, we need to stop its interval ENTER_FRAME and remove it in a manner that it does not excessively consume the user's CPU resources.
so, if its x is bigger then the stage width
if(x>this.parent.parent.stage.stageWidth+width+10)
{
I call the functions fermaIntervallo() e rimuovimi()
fermaIntervallo();
rimuovimi();
}

fermaIntervallo();
I stop the interval ENTER_FRAME
removeEventListener(Event.ENTER_FRAME,go);

rimuovimi();
I tell Trigonometria_2.as (from which the instance of the clip has been created) to remove that clip
this.parent.removeChild(this);

Third example:

I create a FLA and save it as 'trigonometria_3_b.fla'.
Inside it, I create the same MovieClip of circular shape (mc_ball) still in library. I assign to it a linkage ID 'Ball2', the same name of the class which I will create.
I create a Document Class, an AS file saved as 'Trigonometria3.as', implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	import flash.utils.Timer;
	import flash.events.Event;
	import flash.events.TimerEvent;
	
	public class Trigonometria3 extends MovieClip
	{
		private var contenitore_mc:MovieClip;
		
		private var timer:Timer;
		
		private const velocitaTimer:int=100;
		
		public function Trigonometria3():void
		{
			init();
		}
		
		private function init():void
		{
			stage.frameRate=31;
			
			contenitore_mc=new MovieClip();
			addChild(contenitore_mc);
			
			startTimer();
		}
		
		private function startTimer():void
		{
			timer=new Timer(velocitaTimer,0);
			timer.addEventListener(TimerEvent.TIMER,attaccaBall);
			timer.start();
		}
		
		private function attaccaBall(t:TimerEvent):void
		{
			var ball:Ball2=new Ball2();
			contenitore_mc.addChild(ball);
			image_mc.mask=contenitore_mc;
			ball.muoviti();
		}
	}
}
I create a Ball2 class, an AS file saved as 'Ball2.as', implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	
	public class Ball2 extends MovieClip
	{
		private var centroY:Number;
		private var angolo:Number=0;
		private var raggio:Number;
		private var velocita:Number;
		
		public function Ball2():void
		{
			init();
		}
		
		private function init():void
		{
			x=0;
		}
		
		public function muoviti():void
		{
			y=this.parent.parent.stage.stageHeight/2;
			centroY=y;
			raggio=10+Math.random()*(y-10);
			velocita=.5+Math.random()*4.5;
			scaleX=scaleY=.2+Math.random();
			//alpha=.1+Math.random();
			addEventListener(Event.ENTER_FRAME,go);
		}
		
		private function go(e:Event):void
		{
			y=centroY+Math.sin(angolo)*raggio;
			x+=velocita;
			angolo+=.1;
			if(x>this.parent.parent.stage.stageWidth+width+10)
			{
				fermaIntervallo();
				rimuovimi();
			}
		}
		
		private function fermaIntervallo():void
		{
			removeEventListener(Event.ENTER_FRAME,go);
		}
		
		private function rimuovimi():void
		{
			this.parent.removeChild(this);
		}
	}
}
Let's analyse the code.

Trigonometria3.as class

Properties


a MovieClip containing all the instances of the Ball2.as class ( practically instead of attaching the clips to the stage, I will attach them inside an empty MovieClip which will then be used as a mask for image_mc)
private var contenitore_mc:MovieClip;
a timer (instance of the Timer class)
private var timer:Timer;
a constant 'velocitaTimer' with a value equal to the speed of the timer
private const velocitaTimer:int=150;

Methods

init();
I impost the frame rate
stage.frameRate=31;
I create an empty MovieClip
contenitore_mc=new MovieClip();
I add the empty MovieClip to the DisplayObject (otherwise it would not be visible)
addChild(contenitore_mc);
I call the function startTimer()
startTimer();

startTimer();
I create an instance of 'timer' passing the value of the constant 'velocitaTimer' and zero (infinite)
timer=new Timer(velocitaTimer,0);
I add a listener to 'timer' which will call every tot of hundredth of seconds (equal to the value of the constant 'velocitaTimer') the function attaccaBall()
timer.addEventListener(TimerEvent.TIMER,attaccaBal l);
I start the timer
timer.start();

attaccaBall();
I create an instance of the Ball2 class (which will we see next)
var ball:Ball2=new Ball2();
as the Ball2 class extends the MovieClip class, I add the instance of Ball2 class (ball) to the empty MovieClip 'contenitore_mc'
contenitore_mc.addChild(ball);
I assign 'contenitore_mc' as a mask to 'image_mc'
image_mc.mask=contenitore_mc;
I call the function muoviti() of the ball2 class
ball.muoviti();

Ball2.as class
Same as Ball.as

Source files:
Attached Files
File Type: zip trigonometry_3.zip (50.2 KB, 30 views)

__________________

 


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 !

Last edited by Flep; 05-06-08 at 16:40..
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
Trigonometry with Actionscript 3.0 example 5 Flep Tutorials 1 17-07-08 03:14
Trigonometry example 6 - rotatory movement and direction Flep Tutorials 0 03-03-08 08:28
Trigonometry with Actionscript 3.0 [example1] Flep Tutorials 0 29-09-07 10:12
Trigonometry with Actionscript 3.0 [example2] Flep Tutorials 0 27-09-07 21:51
Trigonometry with Actionscript 3.0 - example 4 Flep Tutorials 0 25-09-07 17:21


All times are GMT. The time now is 20:27.


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