Flash8中没有提供画虚线的代码,我自己参照网上写了一个,原理很简单,从某一端点开始,先画一段实线段,跳过某个间隔,在画是线段,直到另一端点结束。实线段的两个端点可以根据三角形相似求出。
// 画虚线:
// a,b为线段端点,style为线段粗细,color为线段颜色,len为小线段的长度,gap为小线段间的间隔
function DrawDashed(a, b, style, color, len, gap)
{
var flag = 0;
var dist = Distance(a, b);
var max = len;
var c = a;
var d = b;
this.lineStyle(style, color, 100);
while (max < dist)
{
// 先画一段实线段CD
d = GetP(a, b, max, dist);
this.moveTo(c.x, c.y);
this.lineTo(d.x, d.y);
max += gap;
if (max < dist)
{
// 跳过间隔,重置C点
c = GetP(a, b, max, dist);
}
else
{
flag = 1;
break;
}
max += len;
}
if (flag != 1)
{
this.moveTo(c.x, c.y);
this.lineTo(b.x, b.y);
}
}
function Distance(a, b)
{
return Math.sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
function GetP(a, b, l, d)
{
var n = new Object();
n.x = l * (b.x - a.x)/d + a.x;
n.y = l * (b.y - a.y)/d + a.y;
return n;
}