Flash Gallery | Flash Templates | Flash Menu | Flash Design | Flash Audio & Video

flash page flip

Actionscript 3.0 video tutorials

+ Reply to Thread
Results 1 to 1 of 1

Thread: Trigonometry with Actionscript 3.0 [example1]

  1. #1
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,609
    Rep Power
    9

    Trigonometry with Actionscript 3.0 [example1]

    flash templates
    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.

+ Reply to Thread

LinkBacks (?)


Similar Threads

  1. Replies: 1
    Last Post: 17-07-08, 02:14
  2. Replies: 0
    Last Post: 03-03-08, 07:28
  3. Replies: 0
    Last Post: 27-09-07, 20:51
  4. Replies: 0
    Last Post: 27-09-07, 09:11
  5. Replies: 0
    Last Post: 25-09-07, 16:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts