`

Flash Player 10 使用绘图API

    博客分类:
  • Flex
阅读更多

 

转自:http://sjkgxf7191.iteye.com/blog/520184

Flash Player 10 使用绘图API

了解 Graphics 类

  • 每个 Shape 、Sprite 和 MovieClip 对象都具有一个 graphics 属性,它是 Graphics 类的一个实例
  • 如果要将显示对象仅用作内容 绘制画布,则可以使用 Shape 实例。
  • Shape 实例的性能优于其它 用于绘制的显示对象,因为它不会产生 Sprite 和 MovieClip 类中的附加功能的开销。
  • 如果希望能够在显示对象上绘制图形内容,并且还希望该对象包含其它显示对象 ,则可以使用 Sprite实例。

矢量

  • 数据类型完全相同 的值组成的数组。
  • Vector 对象可存储绘制方法 使用单个命令构建线条和形状时所用值 的数组。

使用图形数据类

可以将全部绘制存储 在 IGraphicsData 类型的矢量对象数组 (Vector.<IGraphicsData> ) 中,该数组可以重复
用作其它形状实例的数据源,也可以存储绘制信息供以后使用。
GraphicsStroke
GraphicsSolidFill
GraphicsGradientFill
GraphicsShaderFill
GraphicsEndFill
GraphicsPath
GraphicsTrianglePath

路径

?View Code ACTIONSCRIPT3
drawPath(commands:Vector.<int>, data:Vector.<number>)

第一个参数保存命令 ,第二个参数保存数据
第一个参数命令如下:
flash.display.GraphicsPathCommand 类里

public static const NO_OP:int = 0;
public static const MOVE_TO:int = 1;
public static const LINE_TO:int = 2;
public static const CURVE_TO:int = 3;
public static const WIDE_MOVE_TO:int = 4;
public static const WIDE_LINE_TO:int = 5;

画直线 参考代码如下:

?View Code ACTIONSCRIPT3
var commands:Vector.<int> = new Vector.<int>();
commands[0] = GraphicsPathCommand.MOVE_TO;
commands[1] = GraphicsPathCommand.LINE_TO;
 
var data:Vector.<Number> = new Vector.<Number>();
data[0] = 100;
data[1] = 100;
data[2] = 250;
data[3] = 200;
 
graphics.lineStyle(0);
graphics.drawPath(commands, data);

画曲线 参考代码如下:

?View Code ACTIONSCRIPT3
commands.push(GraphicsPathCommand.MOVE_TO);
// 起点是200, 200
data.push(200, 200);
 
data.push(250, 100);
data.push(300, 200);
 
data.push(400, 250);
data.push(300, 300);
 
data.push(250, 400);
data.push(200, 300);
 
data.push(100, 250);
data.push(200, 200);
 
commands.push(GraphicsPathCommand.CURVE_TO);
commands.push(GraphicsPathCommand.CURVE_TO);
commands.push(GraphicsPathCommand.CURVE_TO);
commands.push(GraphicsPathCommand.CURVE_TO);

WIDE_LINT_TO模式 参考代码如下:

?View Code ACTIONSCRIPT3
data.push(200, 200);
 
data.push(250, 100);
data.push(300, 200);
 
data.push(400, 250);
data.push(300, 300);
 
data.push(250, 400);
data.push(200, 300);
 
data.push(100, 250);
data.push(200, 200);
 
lineCommands.push(GraphicsPathCommand.MOVE_TO);
// 使用WIDE_LINE_TO命令可以跳过data中的前一组坐标,跟CURVE_TO同步
lineCommands.push(GraphicsPathCommand.WIDE_LINE_TO);
lineCommands.push(GraphicsPathCommand.WIDE_LINE_TO);
lineCommands.push(GraphicsPathCommand.WIDE_LINE_TO);
lineCommands.push(GraphicsPathCommand.WIDE_LINE_TO);

缠绕

顺时针 :正向 缠绕;逆时针 :负向 缠绕
一次beginFill/endFill 时,出现路径交叉 ,那么默认是不填充 交叉部分的
GraphicsPathWinding.EVEN_ODD :默认情况

?View Code ACTIONSCRIPT3
graphics.drawPath(commands, data1, GraphicsPathWinding.EVEN_ODD);

GraphicsPathWinding.NON_ZERO :
如果两个路径的缠绕一致 (都或正或者负),则填充 交叉区域。
如果两个路径的缠绕不一致 (一正一负),则不填充 交叉区域。

?View Code ACTIONSCRIPT3
graphics.drawPath(commands, data1, GraphicsPathWinding.NON_ZERO);

三角形

一个 drawTriangles参数

?View Code ACTIONSCRIPT3
var vertices:Vector.<Number> = new Vector.<Number>();
vertices.push(100, 100);
vertices.push(200, 100);
vertices.push(150, 200);
 
graphics.lineStyle(0);
graphics.drawTriangles(vertices);

两个 drawTriangles参数,可以减少顶点的总数目

?View Code ACTIONSCRIPT3
var vertices:Vector.<Number> = new Vector.<Number>();
vertices.push(100, 100);
vertices.push(200, 100);
vertices.push(200, 200);
vertices.push(100, 200);
 
var indices:Vector.<int> = new Vector.<int>();
indices.push(0, 1, 2);
indices.push(2, 3, 0);
 
graphics.lineStyle(0);
graphics.drawTriangles(vertices, indices);

uvtData

drawTriangles的第三个可选参数 还是一个Vector (Number型),称作uvtData 。uvt表示了影响位图映射于三角形的三个值。u和v 代表x轴和y轴 的比例,t 在用于3D时代表缩放 。

?View Code ACTIONSCRIPT3
var vertices:Vector.<Number> = new Vector.<Number>();
vertices.push(150, 50);
vertices.push(1000, 100);
vertices.push(800, 600);
vertices.push(100, 200);
 
var uvtData:Vector.<Number> = new Vector.<Number>();
uvtData.push(0, 0);
uvtData.push(1, 0);
uvtData.push(1, 1);
uvtData.push(0, 1);
 
var indices:Vector.<int> = new Vector.<int>();
indices.push(0, 1, 2);
indices.push(2, 3, 0);
 
var bitmap:Bitmap = new ImageClass() as Bitmap;
graphics.beginBitmapFill(bitmap.bitmapData);
graphics.drawTriangles(vertices, indices, uvtData);
graphics.endFill();

三角形和3D

使用三角实现3D的基本策略 :
1. 创建一堆三角结构顶点和索引 。
2. 计算于每个顶点相符的3D坐标点位置 。
3. 使用透视法 计算3D坐标点在屏幕上的坐标 ,作为定点的值。

?View Code ACTIONSCRIPT3
perspectiveScale = focalLength / (focalLength + zPosition)

一个含有x,y,z的坐标点,z坐标用于求出缩放比例。然后用这个比例乘以x,y就能得到点在屏幕上的位置,这个位置也就是顶点的值。可以将这个值作为uvtData 的第三个参数 。

?View Code ACTIONSCRIPT3
var scale:Number = focalLength / (focalLength + zpos + centerZ);
vertices.push(xpos * scale, ypos * scale);
uvtData.push(j / (cols - 1), i / (rows - 1));
uvtData.push(scale);

4. 使用drawTriangles 传入顶点和索引。

culling

drawTriangles的第四个可选参数culling,它是一个字符串,接收"positive","negative","none"三个值。
TriangleCulling.POSITIVE :剔除逆时针 的三角形
TriangleCulling.NEGATIVE :剔除顺时针 的三角形
TriangleCulling.NONE:全部绘制

?View Code ACTIONSCRIPT3
sprite.graphics.drawTriangles(vertices, indices, uvtData, TriangleCulling.NEGATIVE);
分享到:
评论

相关推荐

    Starling Graphics extension 绘图API 扩展

    Starling Graphics扩展是针对Starling框架的一个重要组件,它提供了丰富的绘图API,极大地增强了在移动设备上进行2D图形渲染的能力。Starling是一个高效、跨平台的ActionScript 3库,专门设计用于游戏开发,它利用...

    flash高级显示API

    这些API主要由Adobe Flash Player内部实现,并通过`flash.*`包对外提供。Flash绘图是在`DisplayObject`上进行的,而具体绘制工作则是通过`DisplayObject`中的`graphics`属性来完成。 #### 二、Flash显示API基础 **...

    FLASH as3 图像处理技术教程

    - FlashPlayer升级到10.0版本后,ActionScript3.0的绘图API经历了更深刻的变化。最显著的特性包括图形数据的直接复制、绘图命令的保存和恢复以及使用图形作为笔触的新功能。此外,Shader的引入允许开发者绘制笔触和...

    adobe flash palyer activex 10 debug版

    在压缩包中的`flashplayer_10_ax_debug.exe`文件,是安装这个Debug版本Flash Player的执行程序。用户或开发者需要运行此文件来安装该版本的Flash Player,以便在开发和调试过程中使用其特殊功能。 总结起来,Adobe ...

    Flash 游戏 制作 常用代码 解析

    4. **图形绘制**:Flash提供强大的绘图API,允许开发者通过`Graphics`类绘制线条、形状和填充。同时,使用`BitmapData`类可以处理像素级别的图像操作,这对于游戏中的碰撞检测和特效实现非常重要。 5. **事件处理**...

    VC播放半透明flash

    在资源编辑器中,为对话框添加一个新的ActiveX控件,并将其ID设置为如 IDC_FLASHPLAYER。这将在对话框模板中生成一个占位符,用于放置Flash Player控件。 2. **初始化ActiveX控件**: 在对话框的 OnInitDialog() ...

    vc播放flash(支持透明flash窗口穿透)

    最后,`flash.h`文件可能包含了Flash控件的接口定义,以及必要的Windows API和ActiveX库的包含,以确保与Flash Player ActiveX控件的正确交互。 总之,这个项目展示了如何利用VC++ 2010和Win32 API来实现一个具有...

    flex_api (FLEX接口)

    例如,可以使用绘图API创建自定义图形,或者使用Tween类实现平滑的动画过渡。 8. **网络通信** Flex API包含一系列类用于与服务器进行数据交换,如HTTPService、WebService和RemotingDestination。这些类支持SOAP...

    喜欢FLash游戏研究的进来,一个flash挖金游戏

    游戏设计方面,"挖金游戏"可能采用了常见的2D游戏引擎,利用Flash的绘图API绘制场景和角色,通过ActionScript控制游戏逻辑。游戏可能包含各种关卡,每个关卡有不同的地形、障碍物和金矿分布,增加挑战性和趣味性。...

    flash饼图flash饼图

    在创建饼图时,开发者通常会定义数据结构,计算各扇区的大小,然后利用AS3的绘图API来构建图形。 3. **Flash饼图实现**: - 数据准备:首先,你需要有一份包含各类别数值的数据集。例如,piedata.xml文件可能是...

    [转]M2D的介绍——针对Flash ‘Molehill’ API的GPU加速2D框架

    M2D是一个强大的工具,它降低了使用Flash的Molehill API进行GPU加速2D开发的门槛。通过利用M2D,开发者可以充分利用现代硬件的潜力,创造出更具视觉冲击力和流畅体验的2D内容。结合M2DSamples中的实例,开发者可以更...

    flash小游戏之迷宫

    迷宫的图形化展示则利用了Flash的绘图API,可以轻松地绘制出复杂的迷宫结构,并用不同的颜色和形状区分墙和通道。 **交互体验** Flash小游戏的一大优势就是其优秀的交互性。在《迷宫》中,玩家可以通过键盘或鼠标...

    接金币flash游戏源码

    3. **图形渲染**:Flash提供强大的绘图API,如Graphics类,可以绘制基本形状、线条和填充,以及创建自定义图形对象。 4. **音频处理**:Flash可以播放和处理音频,为游戏添加音效和背景音乐。 在【pickgold】这个...

    FLASH翻书效果FLASH

    1. 图形绘制:使用Flash的绘图API,如Graphics类,绘制书页的形状和阴影,模拟翻页的动态过程。 2. 动画控制:使用Tween类或时间轴来控制书页的平滑过渡,包括翻转速度、角度等。 3. 用户交互:监听鼠标点击或触摸...

    FLASH网站源代码

    在Flash中,用户可以使用绘图工具绘制图形,然后通过时间轴控制各个帧的动作,形成流畅的动画效果。此外,Flash支持逐帧动画、形状补间和动作补间等多种动画类型,提供了一种高效创建动态视觉效果的方式。 2. ...

    flash 源代码 示波器

    3. 绘图:使用Flash的绘图API,如drawRect、drawLine等方法,可以在舞台上绘制出示波器的波形。 4. 动态更新:通过在每个帧或特定间隔更新画面,实现示波器的实时效果。 5. 用户交互:添加按钮或滑块等组件,让用户...

Global site tag (gtag.js) - Google Analytics