Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Trigonometry example 6 - rotatory movement and direction

This is a discussion on Trigonometry example 6 - rotatory movement and direction within the Tutorials forums, part of the English Forums category; Hi! We saw diverse techniques on how to apply the trigonometry to Actionscript 3.0 . With this tutorial, we will ...


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

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  2 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 03-03-08, 08:28
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,446
Rep Power: 6
Flep is on a distinguished road
Trigonometry example 6 - rotatory movement and direction

Hi!


We saw diverse techniques on how to apply the trigonometry to Actionscript 3.0 .


With this tutorial, we will see how to manage the rotatory movement of a MovieClip and the direction of movement.

I will clearly make a purely didactic example and as always it will depend on ourselves to find a practical application.

The more fantasy and the more graphics skills, the more you could use those simple trigonometric formulas with wonderful results.


I create a FLA and I save it as "trigonometria6.fla".

Into which I create a MovieClip of round shape, drag it on stage and I give it an instance name "clip_mc".

On an another level, that I will call "code", I apply the following Actionscript:


Code:
stage.frameRate=24;

clip_mc.addEventListener(Event.ENTER_FRAME,rotate);

function rotate(evt:Event):void
{
	var angle:Number=(mouseX-stage.stageWidth/2)*.001;
	var sine:Number=Math.sin(angle);
	var cosine:Number=Math.cos(angle);
	var xx:Number=evt.target.x-stage.stageWidth/2;
	var yy:Number=evt.target.y-stage.stageHeight/2;
	var x1:Number=cosine*xx-sine*yy;
	var y1:Number=sine*xx+cosine*yy;
	evt.target.x=stage.stageWidth/2+x1;
	evt.target.y=stage.stageHeight/2+y1;
}

with the following result (move the mouse above the SWF):









Let us analyse the code


I impost the frame rate

stage.frameRate=24;

I add ENTER_FRAME to clip_mc

clip_mc.addEventListener(Event.ENTER_FRAME,rotate) ;


function rotate(evt:Event):void

{

I create a numerical variable named "angle" that will take the value of an angle given by the difference in between the X position of the mouse and half the stage width, multiplied by 0.001 otherwise it would be too fast. If the value results negative, the change of direction of the rotatory movement of "clip_mc" happens.

var angle:Number=(mouseX-stage.stageWidth/2)*.001;

I create two numerical variables to which I assign the respective value of sinus and co sinus of the above angle

var sine:Number=Math.sin(angle);

var cosine:Number=Math.cos(angle);

I create a numerical variable to which I assign the value of the difference in between the X position of "clip_mc" and half the stage width

var xx:Number=evt.target.x-stage.stageWidth/2;

I create a numerical variable to which I assign the difference in between the Y position of "clip_mc" and half the stage height

var yy:Number=evt.target.y-stage.stageHeight/2;

Now, I use two formulas about the opposite angles found in trigonometry: the theorem of the sinus that says:

The side of a triangle are proportional to the sinus of the opposite angles.

I create a numerical variable to which I assign the value of the co sinus multiplied by xx (the difference in between the X position of "clip_mc" and half the stage height)

var x1:Number=cosine*xx-sine*yy;

Now, I do the opposite

var y1:Number=sine*xx+cosine*yy;

Next I feed to the X of "clip_mc" the value of half the stage width plus the value of the result for variable x1

evt.target.x=stage.stageWidth/2+x1;

And to the Y of "clip_mc", I pass the value of half the stage height plus the value of the result for the variable y1

evt.target.y=stage.stageHeight/2+y1;

}


See you soon & 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 !

Last edited by Flep; 28-08-08 at 06:39..
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 with Actionscript 3.0 [example1] Flep Tutorials 0 29-09-07 10:12
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 21:04.


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