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 3 of 3

Thread: Drawing polygons with Actionscript 3.0

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

    Drawing polygons with Actionscript 3.0

    amazing Flash templates
    Greetings to all!

    I created an Actionscript 3.0 class which allows to draw polygons.
    Other then the practical use of it, this class is purely educational. It is a good example for those of you who wants to learn OOP programming and read the Actionscript 3.0 tutorials of FlepStudio .

    As any class, it has its proper methods and events:

    Methods:
    drawPolygon(radius:int,segments:int,centerX:Number ,centerY:Number,tint:uint,line:int,rotating:Number );
    radius is the dimension of the polygon
    segments are the segment numbers in the polygon
    centerX and centerY are the coordinates where I want the polygon to be drawn
    tint is the colour of the polygon
    line is the thickness of the drawing line
    rotating is to rotate the polygon

    fadeIn();
    performs a fade/in of the polygon

    fadeOut();
    performs a fade/out of the polygon

    Events:
    'fadeInDone' , is returned when the method fadeIN is completed
    'fadeOutDone' is returned when the method fadeOUT is completed

    This is the Polygon.as
    Code:
    /*
     *************************************
      Polygon class                              
      http://www.FlepStudio.org     
      © Author: Filippo Lughi                                      
     *************************************
     */
    package
    {
    	import flash.display.MovieClip;
    	import flash.events.Event;
    	
    	public class Polygon extends MovieClip
    	{
    		//PROPERTIES
    		private var points:Array;
    		
    		private var id:int;
    		private var ratio:Number;
    		private var top:Number;
    		
    		private var fade_value:int;
    		
    		//CONSTRUCTOR
    		public function Polygon()
    		{
    			addEventListener(Event.ADDED_TO_STAGE,init);
    		}
    		
    		private function init(evt:Event):void
    		{
    			removeEventListener(Event.ADDED_TO_STAGE,init);
    			stage.frameRate=31;
    		}
    		
    		//METHODS
    		public function drawPolygon(radius:int,segments:int,centerX:Number,centerY:Number,tint:uint,line:int,rotating:Number):void
    		{
    			id=0;
    			points=new Array();
    			ratio=360/segments;
    			top=centerY-radius;
    			for(var i:int=rotating;i<=360+rotating;i+=ratio)
    			{
    				var xx:Number=centerX+Math.sin(radians(i))*radius;
    				var yy:Number=top+(radius-Math.cos(radians(i))*radius);
    				points[id]=new Array(xx,yy);
    				if(id>=1) 
    				{
    					drawing(points[id-1][0],points[id-1][1],points[id][0],points[id][1],tint,line);
    				}
    				id++;
    			}
    			id=0;
    		}
    		
    		private function radians(n:Number):Number 
    		{
    			return(Math.PI/180*n);
    		}
    		
    		private function drawing(startX:Number,startY:Number,stopX:Number,stopY:Number,tint:Number,line:int):void 
    		{
    			graphics.lineStyle(line,tint,1);
    			graphics.moveTo(startX,startY);
    			graphics.lineTo(stopX,stopY);
    		}
    		
    		public function fadeOut():void
    		{
    			fade_value=0;
    			addEventListener(Event.ENTER_FRAME,fade);
    		}
    		public function fadeIn():void
    		{
    			fade_value=1;
    			addEventListener(Event.ENTER_FRAME,fade);
    		}
    		
    		//PRIVATE
    		private function fade(evt:Event):void
    		{
    			var da:Number=fade_value-alpha;
    			var aa:Number=da*.1;
    			alpha+=aa;
    			if(Math.abs(da)<=.05)
    			{
    				alpha=fade_value;
    				removeEventListener(Event.ENTER_FRAME,fade);
    				if(fade_value==0)
    					dispatchEvent(new Event('fadeOutDone'));
    				else
    					dispatchEvent(new Event('fadeInDone'));
    			}
    		}
    	}
    }
    Example 1:
    Code:
    var polygon:Polygon=new Polygon();
    addChild(polygon);
    polygon.drawPolygon(100,3,100,100,0xFFFFFF,1,0);
    
    var polygon2:Polygon=new Polygon();
    addChild(polygon2);
    polygon2.drawPolygon(50,4,stage.stageWidth/2,stage.stageHeight/2,0x33FFFF,3,0);
    
    var polygon3:Polygon=new Polygon();
    addChild(polygon3);
    polygon3.drawPolygon(80,5,400,300,0xFF0000,6,0);
    the result






    Example 2:
    Code:
    var polygons:int=20;
    
    for(var i:int=0;i < polygons;i++)
    {
    	var polygon:Polygon=new Polygon();
    	addChild(polygon);
    	var distance:int=20+10*i;
    	polygon.drawPolygon(distance,6,distance,distance,0xFFFFFF,1,0);
    }
    the result






    Example 3:
    Code:
    import flash.events.TimerEvent;
    var my_array:Array=new Array();
    var timer:Timer;
    var polygons:int=40;
    
    for(var i:int=0;i < polygons;i++)
    {
    	var polygon:Polygon=new Polygon();
    	polygon.addEventListener('fadeInDone',reverse);
    	polygon.addEventListener('fadeOutDone',reverse);
    	my_array.push(polygon);
    	polygon.alpha=0;
    	addChild(polygon);
    	var distance:int=10+5*i;
    	polygon.drawPolygon(distance,6,distance,distance,0xFFFFFF,1,0);
    }
    timer=new Timer(50,polygons);
    timer.addEventListener(TimerEvent.TIMER,go);
    timer.start();
    
    function go(evt:TimerEvent):void
    {
    	my_array[evt.target.currentCount-1].fadeIn();
    }
    
    function reverse(evt:Event):void
    {
    	if(evt.type=='fadeInDone')
    		evt.target.fadeOut();
    	else
    		evt.target.fadeIn();
    }
    the result






    See you soon !

  2. #2
    Junior Member Settled In TiWind is on a distinguished road
    Join Date
    Aug 2008
    Posts
    1
    Rep Power
    0

    Re: Drawing polygons with Actionscript 3.0

    Hi flep) nice class.. I want to digg it for some new fitures but i cant make it worked. It wrote back that my main swf contains invalid data. I used code but I still cant find where i mistaken.. Hope you can upload your lesson or help with answer... thanx!

  3. #3
    Junior Member Settled In sarahmarie is on a distinguished road
    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0

    Re: Drawing polygons with Actionscript 3.0

    I need to be able to draw a filled polygon. How would i adapt this code to add a fill color?

+ Reply to Thread

Similar Threads

  1. Drawing API
    By PBKHouston in forum advanced Actionscript 3.0
    Replies: 1
    Last Post: 20-02-09, 13:07
  2. Drawing with actionscript 3.0
    By Flep in forum Tutorials
    Replies: 0
    Last Post: 03-10-07, 19:34
  3. Drawing with Actionscript 3.0 - script 2
    By Flep in forum Tutorials
    Replies: 0
    Last Post: 27-09-07, 21:00
  4. Drawing with Actionscript 3.0 - script 3
    By Flep in forum Tutorials
    Replies: 0
    Last Post: 27-09-07, 09:41

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

Search Engine Optimization by vBSEO