Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Trigonometry with Actionscript 3.0 [example2]

This is a discussion on Trigonometry with Actionscript 3.0 [example2] within the Tutorials forums, part of the Flash English category; Here we are! We are back to the trigonometry (very fascinating eh...) This article is the second , but not least, ...


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

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

Here we are! We are back to the trigonometry (very fascinating eh...)
This article is the second , but not least, part of the article Trigonometry with Actionscript 3.0 [example1].
This time, I will apply the trigonometry to the spring effect .
Let's get straight into it...

I create a FLA and save it as 'trigonometria_2.fla' in which:
- I create a Movie Clip with a shape of your liking (it is only an example) with an instance name 'stella_mc'.
- I create another Movie Clip called 'freccia_mc' (in this case an arrow shape) which I will instance twice with the following instance name 'freccia_0_mc' and 'freccia_1_mc'

I create a Document Class, an AS file saved as 'TrigoDue.as', implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	
	public class TrigoDue extends MovieClip
	{
		private var angle:Number=0;
		private const raggio:int=100;
		
		public function TrigoDue()
		{
			init();
		}
		
		private function init():void
		{
			stage.frameRate=31;
			
			stella_mc.x=stage.stageWidth/2;
			stella_mc.y=stage.stageHeight/2;
			
			stella_mc.addEventListener(Event.ENTER_FRAME,muovi);
		}
		
		private function muovi(e:Event):void
		{
			e.target.y=stage.stageHeight/2+Math.sin(angle)*raggio;
			e.target.x=stage.stageWidth/2+Math.sin(angle)*raggio;
			var dx_0:Number=e.target.x-freccia_0_mc.x;
			var dy_0:Number=e.target.y-freccia_0_mc.y;
			var radians:Number=Math.atan2(dy_0,dx_0);
			freccia_0_mc.rotation=(radians/Math.PI)*180;
			var dx_1:Number=e.target.x-freccia_1_mc.x;
			var dy_1:Number=e.target.y-freccia_1_mc.y;
			var radians2:Number=Math.atan2(dy_1,dx_1);
			freccia_1_mc.rotation=(radians2/Math.PI)*180;
			angle+=.1;
		}
	}
}
The result:







Let's analise the code:

Properties


a numerical variable with the angle's value
private var angle:Number=0;
a numerical constant called 'raggio' with the assigned value of 100 (it will be the range of action of stella_mc)
private const raggio:int=100;

Methods
init();
I declare the frame rate
stage.frameRate=31;
I position stella_mc
stella_mc.x=stage.stageWidth/2;
stella_mc.y=stage.stageHeight/2;
I add a listener to the ENTER_FRAME which will call muovi()
stella_mc.addEventListener(Event.ENTER_FRAME,muovi );

muovi();
Let's remember that we are inside the interval (ENTER_FRAME), so this method will be repeated as many times by second as the value of the frame rate
e.target is who has called the event, in this case stella_mc
I assign to the x of stella_mc a value which is half of the stage's height + il sinus of the angle (variable 'angle' with a value of zero) multiplied by the value of the constant 'raggio'
e.target.x=stage.stageWidth/2+Math.sin(angle)*100;
I assign to the y of stella_mc a value which is half of the stage's width + il sinus of the angle (variable 'angle' with a value of zero) multiplied by the value of the constant 'raggio'
e.target.y=stage.stageHeight/2+Math.sin(angle)*100;
I create 2 numerical variables that contain as value the difference between the coordinates of stella_mc and freccia_0_mc
var dx_0:Number=e.target.x-freccia_0_mc.x;
var dy_0:Number=e.target.y-freccia_0_mc.y;
I calculate the radians in between the values of the two variables just created (see this article)
var radians:Number=Math.atan2(dy_0,dx_0);
I assign the degrees (gotten by the matematical formula of conversion radiants/degrees) to the rotation of freccia_0_mc
freccia_0_mc.rotation=(radians/Math.PI)*180;
same thing for freccia_1_mc
var dx_1:Number=e.target.x-freccia_1_mc.x;
var dy_1:Number=e.target.y-freccia_1_mc.y;
var radians2:Number=Math.atan2(dy_1,dx_1);
freccia_1_mc.rotation=(radians2/Math.PI)*180;
I increase the value of 'angle' of 0.1
angle+=.1;

Stay tuned !
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
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 [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 19:11.

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