Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Trigonometry with Actionscript 3.0 [example1]

This is a discussion on Trigonometry with Actionscript 3.0 [example1] within the Tutorials forums, part of the English Forums category; Applying some trigonometry to Actionscript, we can realise some really nice effects and give the possibility to the user to ...


Go Back   Forum Flash CS3 Flash CS4 > English Forums > 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 29-09-07, 10:12
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,446
Rep Power: 6
Flep is on a distinguished road
Trigonometry with Actionscript 3.0 [example1]

Applying some trigonometry to Actionscript, we can realise some really nice effects and give the possibility to the user to interact in an amusing and creative way within our flash application.
The following example will be the first in a series in which I will try to do my best to apply to Actionscript everything I know about trigonometry.
In this case, I am using the static method Math.atan2.

Let's take a look at it'

I create a FLA and save it as 'trigonometria_1.fla' into which I create a Movie Clip in the shape of an arrow and to which I assign an instance name 'freccia_mc'
I create a Document Class, an AS file saved as 'TrigoUno.as', implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	
	public class TrigoUno extends MovieClip
	{
		private var distanzaX:Number;
		private var distanzaY:Number;
		private var radianti:Number;

		public function TrigoUno()
		{
			init();
		}
		
		private function init():void
		{
			stage.frameRate=31;
			
			freccia_mc.x=stage.stageWidth/2;
			freccia_mc.y=stage.stageHeight/2;
			freccia_mc.addEventListener(Event.ENTER_FRAME,ruota);
		}
		
		private function ruota(e:Event):void
		{
			distanzaX=mouseX-freccia_mc.x;
			distanzaY=mouseY-freccia_mc.y;
			radianti=Math.atan2(distanzaY,distanzaX);
			freccia_mc.rotation=(radianti/Math.PI)*180;
		}
	}
}
Here is the result:







Let's analyse what's happening:







I start an interval ( ENTER_FRAME )
freccia_mc.addEventListener(Event.ENTER_FRAME,ruot a);
Inside this interval:
I assign to the variables 'distanzaX' and 'distanzaY' the distance between the mouse coordinates and the arrow.
distanzaX=mouseX-freccia_mc.x;
distanzaY=mouseY-freccia_mc.y;
I assign to the variable 'radianti' the value obtained via the Math.atan2 method, to which I pass the values of the distance e which returns the angle in radiant.
radianti=Math.atan2(distanzaY,distanzaX);
Keeping in mind that the rotation property of the Movie Clip only accepts values in degrees, I convert the radians in degrees using the mathematical formula:
Degrees=(radians divided by PI Greek).
The PI Greek is given to me by flash using the Static Method Math.PI. To finish, I pass the obtained value to the rotation of the arrow.
freccia_mc.rotation=(radianti/Math.PI)*180;

Now, let your fantasy loose and have fun.
__________________

 


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; 28-08-08 at 06:59..
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 [example2] Flep Tutorials 0 27-09-07 21:51
Trigonometry with Actionscript 3.0 [example3] Flep Tutorials 0 27-09-07 10:11
Trigonometry with Actionscript 3.0 - example 4 Flep Tutorials 0 25-09-07 17:21


All times are GMT. The time now is 22:54.


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