Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Drawing polygons with Actionscript 3.0

This is a discussion on Drawing polygons with Actionscript 3.0 within the Tutorials forums, part of the English Forums category; Greetings to all! I created an Actionscript 3.0 class which allows to draw polygons. Other then the practical use ...


Go Back   Forum Flash CS3 Flash CS4 > English Forums > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 04-12-07, 06:43
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,446
Rep Power: 6
Flep is on a distinguished road
Drawing polygons with Actionscript 3.0

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 !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)  
Old 28-09-08, 00:10
Junior Member
 
Join Date: Aug 2008
Posts: 1
Rep Power: 0
TiWind is on a distinguished road
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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Drawing with actionscript 3.0 Flep Tutorials 0 03-10-07 19:34
Drawing with Actionscript 3.0 - script 2 Flep Tutorials 0 27-09-07 21:00
Drawing with Actionscript 3.0 - script 3 Flep Tutorials 0 27-09-07 09:41


All times are GMT. The time now is 19:35.


Powered by vBulletin versione 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap


FlepStudio
by Filippo Lughi
P.IVA 03605860406