仅供参考:
package{
import flash.display.Graphics;
import flash.geom.Rectangle;
public function drawDottedRect(graphics:Graphics, rect:Rectangle){
var sizeOfDash:Number = 5; //size of each dash
graphics.clear();
graphics.lineStyle(1,0x000000,100);
var iterations:Number = Math.ceil(Math.abs(rect.right - rect.left)/(sizeOfDash*2));
for(var i=0;i<iterations;i++){//Draw top
if(rect.right < rect.left){
var moveDist:Number = -(sizeOfDash*2*i);
var dash:Number = -(sizeOfDash);
}else{
var moveDist:Number = sizeOfDash*2*i;
var dash:Number = sizeOfDash;
}
graphics.moveTo(rect.left + moveDist,rect.top);
if(i == iterations-1){
graphics.lineTo(rect.right,rect.top);
}else{
graphics.lineTo(rect.left + moveDist + dash,rect.top);
}
}
for(var i=0;i<iterations;i++){//Draw Bottom
if(rect.right < rect.left){
var moveDist:Number = -(sizeOfDash*2*i);
var dash:Number = -(sizeOfDash);
}else{
var moveDist:Number = sizeOfDash*2*i;
var dash:Number = sizeOfDash;
}
graphics.moveTo(rect.left + moveDist,rect.bottom);
if(i == iterations-1){
graphics.lineTo(rect.right,rect.bottom);
}else{
graphics.lineTo(rect.left + moveDist + dash,rect.bottom);
}
}
var iterations:Number = Math.ceil(Math.abs(rect.bottom - rect.top)/(sizeOfDash*2));
for(var i=0;i<iterations;i++){//Draw left
if(rect.top > rect.bottom){
var moveDist:Number = -(sizeOfDash*2*i);
var dash:Number = -(sizeOfDash);
}else{
var moveDist:Number = sizeOfDash*2*i;
var dash:Number = sizeOfDash;
}
graphics.moveTo(rect.left,rect.top + moveDist);
if(i == iterations-1){
graphics.lineTo(rect.left,rect.bottom);
}else{
graphics.lineTo(rect.left,rect.top + moveDist + dash);
}
}
for(var i=0;i<iterations;i++){//Draw right
if(rect.top > rect.bottom){
var moveDist:Number = -(sizeOfDash*2*i);
var dash:Number = -(sizeOfDash);
}else{
var moveDist:Number = sizeOfDash*2*i;
var dash:Number = sizeOfDash;
}
graphics.moveTo(rect.right,rect.top + moveDist);
if(i == iterations-1){
graphics.lineTo(rect.right,rect.bottom);
}else{
graphics.lineTo(rect.right,rect.top + moveDist + dash);
}
}
}
}
import flash.display.Graphics;
import flash.geom.Rectangle;
public function drawDottedRect(graphics:Graphics, rect:Rectangle){
var sizeOfDash:Number = 5; //size of each dash
graphics.clear();
graphics.lineStyle(1,0x000000,100);
var iterations:Number = Math.ceil(Math.abs(rect.right - rect.left)/(sizeOfDash*2));
for(var i=0;i<iterations;i++){//Draw top
if(rect.right < rect.left){
var moveDist:Number = -(sizeOfDash*2*i);
var dash:Number = -(sizeOfDash);
}else{
var moveDist:Number = sizeOfDash*2*i;
var dash:Number = sizeOfDash;
}
graphics.moveTo(rect.left + moveDist,rect.top);
if(i == iterations-1){
graphics.lineTo(rect.right,rect.top);
}else{
graphics.lineTo(rect.left + moveDist + dash,rect.top);
}
}
for(var i=0;i<iterations;i++){//Draw Bottom
if(rect.right < rect.left){
var moveDist:Number = -(sizeOfDash*2*i);
var dash:Number = -(sizeOfDash);
}else{
var moveDist:Number = sizeOfDash*2*i;
var dash:Number = sizeOfDash;
}
graphics.moveTo(rect.left + moveDist,rect.bottom);
if(i == iterations-1){
graphics.lineTo(rect.right,rect.bottom);
}else{
graphics.lineTo(rect.left + moveDist + dash,rect.bottom);
}
}
var iterations:Number = Math.ceil(Math.abs(rect.bottom - rect.top)/(sizeOfDash*2));
for(var i=0;i<iterations;i++){//Draw left
if(rect.top > rect.bottom){
var moveDist:Number = -(sizeOfDash*2*i);
var dash:Number = -(sizeOfDash);
}else{
var moveDist:Number = sizeOfDash*2*i;
var dash:Number = sizeOfDash;
}
graphics.moveTo(rect.left,rect.top + moveDist);
if(i == iterations-1){
graphics.lineTo(rect.left,rect.bottom);
}else{
graphics.lineTo(rect.left,rect.top + moveDist + dash);
}
}
for(var i=0;i<iterations;i++){//Draw right
if(rect.top > rect.bottom){
var moveDist:Number = -(sizeOfDash*2*i);
var dash:Number = -(sizeOfDash);
}else{
var moveDist:Number = sizeOfDash*2*i;
var dash:Number = sizeOfDash;
}
graphics.moveTo(rect.right,rect.top + moveDist);
if(i == iterations-1){
graphics.lineTo(rect.right,rect.bottom);
}else{
graphics.lineTo(rect.right,rect.top + moveDist + dash);
}
}
}
}
附:
public function drawDashedLine(p:IPen, xA:int, yA:int, xB:int, yB:int, dashLength:int):void
{
var u1:Number = xB - xA;
var u2:Number = yB - yA;
var length:Number = Math.sqrt(u1 * u1 + u2 * u2);
var n:int = Math.round( (length / dashLength + 1) / 2);//得到分割的块数
var v1:Number = u1 / (2 * n - 1);//得到X方向增量
var v2:Number = u2 / (2 * n - 1);//得到Y方向增量
for (var i:uint = 0; i < n; i++)
{
var x1:Number = xA + 2 * i * v1;
var y1:Number = yA + 2 * i * v2;
var x2:Number = x1 + v1;
var y2:Number = y1 + v2;
drawLine(p,Math.round(x1), Math.round(y1),Math.round(x2), Math.round(y2));
}
}