Today, we are back to the trigonometry applied to Actionscript 3.0.
In this example of drawing in runtime I used sinus and co sinus.
Before starting this tutorial, I recommend to those who have not done so yet, to read the articles in the section Trigonometry and Flash CS3 . It would then be easier for you, to follow this article.
Let us look at it" I create a FLA and save it as "trigonometria5.fla".
I create a Document Class, an AS file saved as "Trigo5.as", implemented the following way:
Code:
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class Trigo5 extends MovieClip
{
private var clips_array:Array;
private var numclips_array:int=4;
private var centerX:Number;
private var centerY:Number;
private const range:int=50;
private var speed_0:Number=1;
private var angle_0:Number=0;
private var angle_1:Number=0;
public function Trigo5()
{
init();
}
private function init():void
{
stage.frameRate=31;
clips_array=new Array();
centerX=stage.stageWidth-100;
centerY=stage.stageHeight/2;
createClips();
initGraphics();
addEventListener(Event.ENTER_FRAME,go);
}
private function createClips():void
{
for(var i:int=0;i < numclips_array;i++)
{
var clip:MovieClip=new MovieClip();
clips_array.push(clip);
addChild(clip);
}
}
private function initGraphics():void
{
clips_array[0].graphics.moveTo(0,stage.stageHeight/2);
clips_array[0].graphics.lineStyle(3,0xFFFFFF,100);
clips_array[1].graphics.moveTo(0,stage.stageHeight/2);
clips_array[1].graphics.lineStyle(3,0xFFFFFF,100);
clips_array[2].graphics.moveTo(0,stage.stageHeight/2+50);
clips_array[2].graphics.lineStyle(3,0xFFFFFF,100);
clips_array[3].graphics.moveTo(0,stage.stageHeight/2-50);
clips_array[3].graphics.lineStyle(3,0xFFFFFF,100);
}
private function go(evt:Event):void
{
clips_array[0].graphics.lineTo(speed_0,centerY+Math.sin(angle_0)*range);
clips_array[1].graphics.lineTo(speed_0,centerY+Math.sin(angle_1)*range);
clips_array[2].graphics.lineTo(speed_0,centerY+Math.cos(angle_0)*range);
clips_array[3].graphics.lineTo(speed_0,centerY-Math.cos(angle_1)*range);
angle_0+=.2;
angle_1-=.2;
speed_0+=2;
}
}
}
The result:
Let us analyse the code
Properties
An Array into which I insert the clips created
private var clips_array:Array;
a variable, which contains as value the number of clips, wanted
private var numclips_array:int=4;
two variables to which I assign two fixed values based on the stage dimension
private var centerX:Number;
private var centerY:Number;
a constant which contains as a numerical value the range
private const range:int=50;
a numeral variable which I will increase and use as horizontal speed of the draw
private var speed_0:Number=1;
two numerical variables which used for the angles of circumference
private var angle_0:Number=0;
private var angle_1:Number=0;
Methods
init();
I impost the frame rate
stage.frameRate=31;
I initialize the Array
clips_array=new Array();
I assign a value to centerX and centerY
centerX=stage.stageWidth-100;
centerY=stage.stageHeight/2;
I call the methods "createClips" which create the clips
createClips();
I call the method "initGraphics" which will initialise the graphics part of the MovieClips
initGraphics();
I add a listener to the event ENTER_FRAME which will call the method "go"
addEventListener(Event.ENTER_FRAME,go);
createClips();
using a "for" cycle with a maximum iteration equal to the values of "numClips", I create the MovieCLips and I add them to the array then to the stage
for(var i:int=0;i < numclips_array;i++)
{
var clip:MovieClip=new MovieClip();
clips_array.push(clip);
addChild(clip);
}
initGraphics();
I initialise the graphics part of each MovieClip at a precise point (you could change some of those values to better understand the use)
clips_array[0].graphics.moveTo(0,stage.stageHeight/2);
clips_array[0].graphics.lineStyle(3,0xFFFFFF,100);
clips_array[1].graphics.moveTo(0,stage.stageHeight/2);
clips_array[1].graphics.lineStyle(3,0xFFFFFF,100);
clips_array[2].graphics.moveTo(0,stage.stageHeight/2+50);
clips_array[2].graphics.lineStyle(3,0xFFFFFF,100);
clips_array[3].graphics.moveTo(0,stage.stageHeight/2-50);
clips_array[3].graphics.lineStyle(3,0xFFFFFF,100);
go();
in the interval ENTER_FRAME, the method is called as many times by seconds based on the frame rate.
Each clip will now start drawing, using the value of "speed_0" as x coordinate (then increased smoothly) and using as a y coordinate the value of the sinus or cosines of one of the angles "angle_0" or "angle_1" multiplied by the value of "range"
clips_array[0].graphics.lineTo(speed_0,centerY+Math.sin(angle_0) *range);
clips_array[1].graphics.lineTo(speed_0,centerY+Math.sin(angle_1) *range);
clips_array[2].graphics.lineTo(speed_0,centerY+Math.cos(angle_0) *range);
clips_array[3].graphics.lineTo(speed_0,centerY-Math.cos(angle_1)*range);
I increase the value of the angles
angle_0+=.2;
angle_1-=.2;
I increase the speed
speed_0+=2;
See you soon!
Bookmarks