#1 (permalink)  
Old 29-09-07, 09:12
Administrator
Living At The FlepStudio!
 
Join Date: Jul 2007
Location: Cesenatico
Posts: 4,917
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.
__________________
_________________________________________
VIDEO CORSI ACTIONSCRIPT 3.0 creati da FlepStudio
I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread please !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 28-08-08 at 05:59.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
sponsor links
Flashmint flash templates FlippingBook-PDF publisher Flash Media Server Hosting
sponsor links
Reply

Bookmarks

Tags
actionscript, example1, trigonometry

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 02:14
Trigonometry example 6 - rotatory movement and direction Flep Tutorials 0 03-03-08 07:28
Trigonometry with Actionscript 3.0 [example2] Flep Tutorials 0 27-09-07 20:51
Trigonometry with Actionscript 3.0 [example3] Flep Tutorials 0 27-09-07 09:11
Trigonometry with Actionscript 3.0 - example 4 Flep Tutorials 0 25-09-07 16:21



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
vBulletin Skin developed by: vBStyles.com
FlepStudio 2007-2009