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 !