·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]AS3画正多边形源代码示例

时间:2013-03-29 23:21pgoy.co.uk
[AS3]AS3画正多边形源代码示例,绘制一个有填充颜色的正五边形,将多边形对象添加到舞台

调用方法:

  1. import Polygon; //引用脚本 
  2. var my_poly:Polygon=new Polygon(); //实例化一个多边形对象 
  3. /* 
  4. *drawPolygon(radius:int,segments:int,centerX:Number,
    centerY:Number,tint:uint,line:int,rotating:Number,fillColor:uint=0
  5. *radius: 边长, px为单位,正整数, 
  6. *segments:边的个数,正整数 
  7. *centerX: 中心点x坐标 
  8. *centerY: 中心点y坐标 
  9. *tint: 边颜色, 16进制数字 例如:0xFF0000 红色 
  10. *line: 边线宽度,px为单位 
  11. *rotating: 多边形旋转角度, 
  12. *fillColor: 填充颜色,16进制数字 不提供值时不填充。 
  13. */
  14.  
  15. my_poly.drawPolygon(100,7,300,200,0xFF0000,1,0); 
    //CuPlayer.com绘制一个无填充颜色的正七边形 
  16. my_poly.drawPolygon(100,5,300,200,0xFF0000,1,0,0x00FF00); 
    //CuPlayer.com绘制一个有填充颜色的正五边形 
  17. addChild(my_poly); //将多边形对象添加到舞台 
  1. package 
  2.     import flash.display.MovieClip; 
  3.     import flash.events.Event; 
  4.  
  5.     public class Polygon extends MovieClip 
  6.     { 
  7.         //CuPlayer.com PROPERTIES 
  8.         private var points:Array; 
  9.  
  10.         private var id:int; 
  11.         private var ratio:Number; 
  12.         private var top:Number; 
  13.  
  14.         //CuPlayer.com CONSTRUCTOR 
  15.         public function Polygon() 
  16.         { 
  17.             addEventListener(Event.ADDED_TO_STAGE,init); 
  18.         } 
  19.  
  20.         private function init(evt:Event):void 
  21.         { 
  22.             removeEventListener(Event.ADDED_TO_STAGE,init); 
  23.             stage.frameRate=30
  24.         } 
  25.  
  26.         //CuPlayer.com METHODS 
  27.         public function drawPolygon(radius:int,segments:int,
    centerX:Number,centerY:Number,tint:uint,line:int,rotating:Number,fillColor:uint=0):void 
  28.         { 
  29.             id=0
  30.             points=new Array(); 
  31.             ratio=360/segments; 
  32.             top=centerY-radius; 
  33.             if(fillColor!=0) 
  34.                 graphics.beginFill(fillColor); 
  35.             for(var i:int=rotating;i<=360+rotating;i+=ratio) 
  36.             { 
  37.                 var xx:Number=centerX+Math.sin(radians(i))*radius; 
  38.                 var yy:Number=top+(radius-Math.cos(radians(i))*radius); 
  39.                 points[id]=new Array(xx,yy); 
  40.                 switch(id) 
  41.                 { 
  42.                     case 0: 
  43.                         break; 
  44.                     case 1: 
  45.                         graphics.lineStyle(line,tint,1); 
  46.                         graphics.moveTo(points[id-1][0],points[id-1][1]); 
  47.                         graphics.lineTo(points[id][0],points[id][1]); 
  48.                         break; 
  49.                     default: 
  50.                     graphics.lineTo(points[id][0],points[id][1]); 
  51.                     //drawing(points[id-1][0],points[id-1][1],points[id][0],points[id][1],tint,line); 
  52.                 } 
  53.                 id++; 
  54.             } 
  55.             if(fillColor!=0) 
  56.                 graphics.endFill(); 
  57.             id=0
  58.         } 
  59.  
  60.         private function radians(n:Number):Number 
  61.         { 
  62.             return(Math.PI/180*n); 
  63.         } 
  64.  
  65.     } 
  66. //脚本代码结束 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器