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 !
Last edited by Flep; 28-08-08 at 06:39.
Bookmarks