Flash Gallery | Flash Templates | Flash Menu | Flash Design | Flash Audio & Video

flash page flip

Actionscript 3.0 video tutorials

+ Reply to Thread
Results 1 to 1 of 1

Thread: Trigonometry with Actionscript 3.0 - example 4

  1. #1
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,609
    Rep Power
    9

    Trigonometry with Actionscript 3.0 - example 4

    flash templates
    Here is another example that carries on the theme of the trigonometry applied to Actionscript.
    Regarding the code, you will find nothing new that has not already been published in this blog.
    I ‘attach’ MovieClips in runtime as in the article attachMovie removed to which I apply some trigonometry that we have already seen in the first article Trigonometry applied to Actionscript 3.0 .
    It came out of it a very nice effect and it is needless to say that the more imagination you put into it the more you get. :))))







    Let's take a look at the code'

    I create a FLA and save it as 'trigonometria_4.fla', into which I create a MovieClip named 'mc_triangolo'.
    I right click the 'mc_triangolo' in library and I give it the linkage id of 'Triangolo' (a class that I will create next)
    I create a Document Class, an AS file saved as 'Trigo4.as', implemented the following way:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	import flash.utils.Timer;
    	import flash.events.TimerEvent;
    	
    	public class Trigo4 extends MovieClip
    	{
    		public var container_mc:MovieClip;
    		
    		private var timer:Timer;
    		
    		public function Trigo4()
    		{
    			init();
    		}
    		
    		private function init():void
    		{
    			stage.frameRate=31;
    			
    			createContainer();
    			goTimer();
    		}
    		
    		private function createContainer():void
    		{
    			container_mc=new MovieClip();
    			addChild(container_mc);
    		}
    		
    		private function goTimer():void
    		{
    			timer=new Timer(20,0);
    			timer.addEventListener(TimerEvent.TIMER,attachClip);
    			timer.start();
    		}
    		
    		private function attachClip(t:TimerEvent):void
    		{
    			var triangolo:Triangolo=new Triangolo(this);
    			container_mc.addChild(triangolo);
    		}
    	}
    }
    I now create the Triangolo class, an AS file saved as 'Triangolo.as':
    Code:
    package
    {
    	import flash.display.MovieClip;
    	import flash.events.Event;
    	
    	public class Triangolo extends MovieClip
    	{
    		private var Root:MovieClip;
    		
    		private var speed:Number;
    		
    		public function Triangolo(m:MovieClip)
    		{
    			Root=m;
    			
    			init();
    		}
    		
    		private function init():void
    		{
    			speed=2+Math.random()*20;
    			
    			x=-width;
    			y=Math.random()*Root.stage.stageHeight;
    			alpha=speed/20;
    			scaleX=.5+speed/20;
    			scaleY=scaleX;
    			
    			addEventListener(Event.ENTER_FRAME,go);
    		}
    		
    		private function go(e:Event):void
    		{
    			x+=speed;
    			var dx:Number=x-Root.mouseX;
    			var dy:Number=y-Root.mouseY;
    			var radianti:Number=Math.atan2(dy,dx);
    			rotation=(radianti/Math.PI)*180;
    			
    			if(x>=Root.stage.stageWidth+width)
    			{
    				removeEventListener(Event.ENTER_FRAME,go);
    				Root.container_mc.removeChild(this);
    			}
    		}
    	}
    }
    Let's analyse the code

    Trigo4 Class

    Properties

    a MovieClip used as container for all the other MovieClips that I will 'attached'
    public var container_mc:MovieClip;
    a timer
    private var timer:Timer;

    Methods
    init();
    I impost the frame rate
    stage.frameRate=31;
    I call the method createContainer()
    createContainer();
    I call the method goTimer()
    goTimer();
    createContainer();
    I create an instance of container_mc as a new MovieClip
    container_mc=new MovieClip();
    I add the container_mc to the DisplayObject (otherwise it would not be visible)
    addChild(container_mc);

    goTimer();
    I create an instance of a new timer passing the values: 20 as an interval speed and zero as an infinite loop
    timer=new Timer(20,0);
    I add a listener to the event TIMER which will call the function attachClip()
    timer.addEventListener(TimerEvent.TIMER,attachClip );
    I start the timer
    timer.start();

    attachClip();
    I create an instance of the Triangolo class, passing to it the value 'this' (in this case the ex root of Actionscript 2.0)
    var triangolo:Triangolo=new Triangolo(this);
    as the Triangolo Class extends the MovieClip Class, I add the instance to the DisplayObject (otherwise it would not be visible)
    container_mc.addChild(triangolo);

    Triangolo Class

    Properties

    a variable MovieClip to which I assign (the building function) the value of root which comes from the Trigo4 Class
    private var Root:MovieClip;
    a numerical variable
    private var speed:Number;

    Methods
    init();
    I assign to the variable 'speed' a random number in between 2 and 20 (excluded)
    speed=2+Math.random()*20;
    I assign to the x property the negative value of width. As we are inside triangolo_mc placed in the library of trigonometria_4.fla, x=' is referred to it. Each instance of Triangolo.as created by Trigo4 will have the negative value of width
    x=-width;
    I assign to the y property a random number in between 0 and the maximum stage height
    y=Math.random()*Root.stage.stageHeight;
    I assign to the alpha property a value equal to the value of 'speed' divided by 20
    alpha=speed/20;
    I assign to the scaleX property a value equal to 0,5 plus the value of 'speed' divided by 20
    scaleX=.5+speed/20;
    I impost the scaleY property equal to scaleX
    scaleY=scaleX;
    I add an interval ENTER_FRAME which calls the function go() as many times by seconds based on the frame rate
    addEventListener(Event.ENTER_FRAME,go);

    go();
    I increase the x value plus the 'speed'
    x+=speed;
    I apply the basic trigonometry viewed in the article Trigonometry with Actionscript 3.0 ' Example 1
    var dx:Number=x-Root.mouseX;
    var dy:Number=y-Root.mouseY;
    var radianti:Number=Math.atan2(dy,dx);
    rotation=(radianti/Math.PI)*180;
    if x is bigger then the stage width plus this clip width
    if(x>=Root.stage.stageWidth+width)
    {
    I stop the interval for that clip
    removeEventListener(Event.ENTER_FRAME,go);
    Do you remember container_mc' I also remove that clip
    Root.container_mc.removeChild(this);
    }

    Nice isn't it'! :)

+ Reply to Thread

Similar Threads

  1. Replies: 1
    Last Post: 17-07-08, 02:14
  2. Replies: 0
    Last Post: 03-03-08, 07:28
  3. Replies: 0
    Last Post: 29-09-07, 09:12
  4. Replies: 0
    Last Post: 27-09-07, 20:51
  5. Replies: 0
    Last Post: 27-09-07, 09:11

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts