|
|
绘制路径有关绘制线条和曲线的节中(请参阅绘制直线和曲线)介绍了一些命令,它们可用于绘制单个线条 (Graphics.lineTo()) 或曲线 (Graphics.curveTo()),然后将得到的线移至另一点 (Graphics.moveTo()),从而组成形状。Flash Player 10 和 Adobe AIR 1.5 引入了对 ActionScript 绘图 API 增强功能(如 Graphics.drawPath() 和 Graphics.drawTriangles(),这些方法采用现有的绘图命令作为参数)的支持。因此,可以在单个语句中运行一系列 Graphics.lineTo()、Graphics.curveTo() 或 Graphics.moveTo() 命令。 其中的两项绘图 API 增强功能使 Graphics.drawPath() 和 Graphics.drawTriangles() 可以合并现有命令:
不再需要针对每个形状片段使用单独的命令。例如,Graphics.drawPath() 方法将 Graphics.moveTo()、Graphics.lineTo() 和 Graphics.curveTo() 并入一个方法中。无需单独调用每个方法,它们已抽象到 GraphicsPathCommand 类定义的数字标识符中。moveTo() 操作由 1 表示,lineTo() 操作由 2 表示。这些值的数组存储在 Vector.<int> 对象中,供 commands 参数中使用。然后,在 Vector.<Number> 对象中为 data 参数创建包含坐标的另一个数组。每个 GraphicsPathCommand 值都与 data 参数中存储的坐标值相对应,其中两个连续数字定义目标坐标空间中的位置。 注: 矢量中的值不是 Point 对象;该矢量是一系列数字,其中由两个数字组成的每个组表示一个 x/y 坐标对。
Graphics.drawPath() 方法将每个命令与其各自的点值(由两个或四个数字组成的集合)相匹配,以在 Graphics 对象中生成路径: package{
import flash.display.*;
public class DrawPathExample extends Sprite {
public function DrawPathExample(){
var square_commands:Vector.<int> = new Vector.<int>(5,true);
square_commands[0] = 1;//moveTo
square_commands[1] = 2;//lineTo
square_commands[2] = 2;
square_commands[3] = 2;
square_commands[4] = 2;
var square_coord:Vector.<Number> = new Vector.<Number>(10,true);
square_coord[0] = 20; //x
square_coord[1] = 10; //y
square_coord[2] = 50;
square_coord[3] = 10;
square_coord[4] = 50;
square_coord[5] = 40;
square_coord[6] = 20;
square_coord[7] = 40;
square_coord[8] = 20;
square_coord[9] = 10;
graphics.beginFill(0x442266);//set the color
graphics.drawPath(square_commands, square_coord);
}
}
}
|