Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Pythagorean theorem with Actionscript 3.0

This is a discussion on Pythagorean theorem with Actionscript 3.0 within the Tutorials forums, part of the English Forums category; In this article we will see how to apply the Pythagorean theorem to Actionscript 3.0. This theorem allows us ...


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 27-09-07, 21:33
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,446
Rep Power: 6
Flep is on a distinguished road
Pythagorean theorem with Actionscript 3.0

In this article we will see how to apply the Pythagorean theorem to Actionscript 3.0.
This theorem allows us to calculate the distance in between two points keeping in mind both X coordinates and Y coordinates of the points.
The theorem has more then one practical applications while developing with Flash, but we will discover them as we go forward with the articles that I will publish on this matter.
For now, let's concentrate on how to calculate this distance.

Let's see how...

I create a FLA and save it as 'pitagora.fla', into which I position two MovieClip (of whatever shape) and to which I assign the instance names 'clip_0_mc' and 'clip_1_mc'.
I create a Document Class, an AS file saved as 'Pitagora.as', implemented the following way:
Code:
package
{
	import flash.display.MovieClip;
	import flash.text.TextField;
	
	public class Pitagora extends MovieClip
	{
		public function Pitagora()
		{
			calcolaDistanza();
		}
		
		private function calcolaDistanza():void
		{
			var distanzaX:Number=clip_0_mc.x-clip_1_mc.x;
			var distanzaY:Number=clip_0_mc.y-clip_1_mc.y;
			var distanza_reale:Number=Math.sqrt(distanzaX*distanzaX+distanzaY*distanzaY);
			
			distanza_txt.text=distanza_reale.toString();
		}
	}
}
The result:







Lets' analise the code.
The Pythagorean theorem states:
In every right-angled triangle, the surface of the square built on the hypotenuse is equal to the sum of the squares built on the remaining two sides.



For Flash, what is the hypotenuse and what are the remaining two sides?
The first side is formed of the distance in between the X coordinates of the two points (in this case, the X coordinates of the two MovieClip)
The second side is formed of the distance in between the Y coordinates of the two points (in this case, the Y coordinates of the two MovieClip)
The hypotenuse represents the distance that we would like to calculate.
So, starting from the two sides, we simply apply the Pythagorean theorem the following way:

I calculate the length of the first side:
var distanzaX:Number=clip_0_mc.x-clip_1_mc.x;
I calculate the length of the second side:
var distanzaY:Number=clip_0_mc.y-clip_1_mc.y;
NB: Based on the position of the two MovieClip, we could find ourselves with negative values for the sides but the theorem states the square of the length and so whatever squared number (negative or positive) gives a positive number as a result.
I calculate the distance of the hypotenuse (the distance in between the two points) with Math.sqrt (the square of...) of side1 by side1 + side2 by side2
var distanza_reale:Number=Math.sqrt(distanzaX*distanza X+distanzaY*distanzaY);

Finaly I render the distance as a text field (this would be all for this article, we will see later on in other articles what we could do with it)
distanza_txt.text=distanza_reale.toString();

In this other example, I repeat all the calculations inside an interval ENTER_FRAME, drag the balls to see the varying distances.







This is the class I use:
Code:
package
{
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class Pitagora2 extends MovieClip
	{
		public function Pitagora2()
		{
			init();
		}
		
		
		private function init():void
		{
			stage.frameRate=31;
			
			clip_0_mc.buttonMode=true;
			clip_1_mc.buttonMode=true;
			
			this.addEventListener(Event.ENTER_FRAME,calcolaDistanza);
			clip_0_mc.addEventListener(MouseEvent.MOUSE_DOWN,muoviClip);
			clip_0_mc.addEventListener(MouseEvent.MOUSE_UP,fermaClip);
			clip_1_mc.addEventListener(MouseEvent.MOUSE_DOWN,muoviClip);
			clip_1_mc.addEventListener(MouseEvent.MOUSE_UP,fermaClip);
		}
		
		private function calcolaDistanza(e:Event):void
		{
			var distanzaX:Number=clip_0_mc.x-clip_1_mc.x;
			var distanzaY:Number=clip_0_mc.y-clip_1_mc.y;
			var distanza_reale:Number=Math.sqrt(distanzaX*distanzaX+distanzaY*distanzaY);
			
			distanza_txt.text=distanza_reale.toString();
		}
		
		private function muoviClip(m:MouseEvent):void
		{
			m.target.startDrag();
		}
		private function fermaClip(m:MouseEvent):void
		{
			m.target.stopDrag();
		}
	}
}
Stay tuned !
__________________

 


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 !
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
Pythagorean theorem - collision detection Flep Tutorials 0 27-09-07 09:10


All times are GMT. The time now is 23:32.


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