AS3 画扇形函数
最近工作需要,进一步了解了些Flash代码的东西,对AS3更是起了浓厚的兴趣。于是把之前JS写的连连看游戏搬到flash上面来。
当然,flash上很多这种游戏了,个人只是为了学习和进步。现在还没有完善好,过几天再放上来。现在临时研究了点扇形画法的问题。贴出来源代码共享下。
库函数一枚:
package com.shirne.extend { import flash.display.Graphics; public class ExGraphics { public static function drawSector(obj:Graphics,x:Number=0,y:Number=0,radius:Number=100,fromRadian:Number=0,radian:Number=0):void { obj.moveTo(x,y); if(Math.abs(radian) > Math.PI * 2){ obj.drawCircle(x,y,radius); }else{ var n:int = Math.ceil(radian * 4 / Math.PI); var angleAvg:Number = radian / n; var angleMid:Number, bx:Number, by:Number,cx:Number, cy:Number; obj.lineTo(x + radius * Math.cos(fromRadian),y + radius * Math.sin(fromRadian)); for (var i=1; i<=n; i++) { fromRadian += angleAvg; angleMid = fromRadian - angleAvg * .5; bx=x + radius * Math.cos(angleMid) / Math.cos(angleAvg * .5); by=y + radius * Math.sin(angleMid) / Math.cos(angleAvg * .5); cx = x + radius * Math.cos(fromRadian); cy = y + radius * Math.sin(fromRadian); obj.curveTo(bx,by,cx,cy); } obj.lineTo(x,y); } } } }
flash代码一枚:
import flash.display.Sprite; import flash.display.StageScaleMode; import flash.display.StageAlign; import com.shirne.extend.ExGraphics; import flash.events.Event; stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; var sp:Sprite=new Sprite(); addChild(sp); var ag:Number=0,speed:Number=.02,type:Boolean=true; this.addEventListener(Event.ENTER_FRAME,drawAll); function drawAll(e:Event):void { ag += speed; if(ag>=2){ type = !type; speed = -speed; }else if(ag<=0){ type = !type; speed = -speed; } sp.graphics.clear(); sp.graphics.beginFill(0xffaa00,.8); ExGraphics.drawSector(sp.graphics,stage.stageWidth*.5,stage.stageHeight*.5,Math.min(stage.stageWidth,stage.stageHeight)*.25,type ? 0 : Math.PI*(2-ag),Math.PI * ag); sp.graphics.endFill(); }使用方法:
1.把库函数导入到flash类库.或者直接将com/shirne/extend/ExGraphics.as这个目录文件结构放到flash文件目录里。
2.新建一个flash as3文件,把第二段代码全部帖到第一帧的代码里,然后运行就可以了。效果如下: