矢量绘图:

1. 为矢量绘图绘制外边 - graphics.lineStype()

    private createGameScene():void
{
console.log("Runtime start."); var myShape:egret.Shape = new egret.Shape(); myShape.graphics.lineStyle(10, 0x00ff00, 1);
myShape.graphics.beginFill(0xff0000, 1);
myShape.graphics.drawRect(0,0,100,200);
myShape.graphics.endFill();
this.addChild(myShape); console.log("Runtime end.");
}

2. 清空一个显示对象的绘图 - graphics.clear()

3. 绘制圆形 - graphics.drawCircle(0, 0, 50)

4. 画直线:

    private createGameScene():void
{
var myShape:egret.Shape = new egret.Shape();
myShape.graphics.lineStyle(5, 0x00ff00, 1);
myShape.graphics.moveTo(50, 10); //将画笔移动到起点位置
myShape.graphics.lineTo(100, 20); //从起点位置划线到终点
myShape.graphics.endFill();
this.addChild(myShape);
}

5. 贝塞尔曲线:

    private createGameScene():void
{
var myShape:egret.Shape = new egret.Shape();
myShape.graphics.lineStyle(5, 0x00ff00, 1);
myShape.graphics.moveTo(50, 200);      //将画笔移动到起点位置
myShape.graphics.curveTo(150, 50, 250, 200); //指定起始移动方向的交汇点坐标,与终点坐标后进行画线
myShape.graphics.endFill();
this.addChild(myShape);
}

6. 绘制圆弧:

    private createGameScene():void
{
var myShape:egret.Shape = new egret.Shape();
myShape.graphics.lineStyle(5, 0x00ff00, 1);
myShape.graphics.beginFill(0x1122cc);
//圆心坐标、半径、起始弧度、终止弧度,填充的区域是圆弧+圆弧两端点的连接所包含的区域
myShape.graphics.drawArc(200,200,100,Math.PI/3, Math.PI);
myShape.graphics.endFill();
this.addChild(myShape);
}

7. 值得注意的是:graphic 可以用来绘制多个图形,不用一个 graphic 对应一个图形。

遮罩

1. 矩形遮罩:

    private createGameScene():void
{
var shp: egret.Shape = new egret.Shape();
shp.graphics.beginFill(0xff0000);
shp.graphics.drawRect(0,0,100,100);
shp.graphics.endFill();
this.addChild(shp); var shp2: egret.Shape = new egret.Shape();
shp2.graphics.beginFill(0x00ff00);
shp2.graphics.drawCircle(0,0,20);
shp2.graphics.endFill();
this.addChild(shp2);
shp2.x = 20;
shp2.y = 20; var rect: egret.Rectangle = new egret.Rectangle(20,20,30,50); //参数指定的区域是遮罩不遮挡的区域
shp.mask = rect;
}

2. 对象遮罩:可以用一个对象当成另一个对象的遮罩

square.mask = circle;    //像这样直接指定即可

碰撞检测

    private createGameScene():void {

        var infoTest = new egret.TextField();
infoTest.y = 200;
infoTest.text = "碰撞结果";
this.addChild(infoTest); //显示一个文本以展示测试结果 var shp:egret.Shape = new egret.Shape();
shp.graphics.beginFill(0xff0000);
shp.graphics.drawRect(0, 0, 120, 120);
shp.graphics.endFill();
this.addChild(shp); console.log(shp.width); //shp 的 width 与 height 与显示对线的内容相关,这里是 120
console.log(shp.height); var testPoint = new egret.Shape();
testPoint.graphics.beginFill(0x000000);
testPoint.graphics.drawRect(100, 100, 5, 5); //标记一个黑色的边界点
testPoint.graphics.endFill();
this.addChild(testPoint); var isHit:boolean = shp.hitTestPoint(110, 110); //结论:是否碰撞与 shp.graphics 相关,而与 shp.width/height 无关
infoTest.text = "碰撞结果" + isHit; //就算已经 addchild,可以直接修改文本, 结果是 true shp.graphics.clear(); //如果不clear, 重新 drawRect 不会生效
shp.graphics.beginFill(0xff0000);
shp.graphics.drawRect(0, 0, 100, 100);
shp.graphics.endFill();
var isHit:boolean = shp.hitTestPoint(110, 110);
infoTest.text = "碰撞结果" + isHit; //结果是 false
}

  值得注意的是,精确坐标的碰撞检测是非常消耗性能的,尽量少用。

Egret 矢量绘图、遮罩、碰撞检测的更多相关文章

  1. Egret引擎随学随机

    1.纹理集实际上就是将一些零碎的小图放到一张大图当中.游戏中也经常使用到纹理集.使用纹理集的好处很多,我们通过将大量的图片拼合为一张图片从而减少网络请求,原先加载数次的图片资源现在加载一次即可.同时, ...

  2. 圆形CD绘制 (扇形)

    参考: Egret教程Arc是使用示例:http://edn.egret.com/cn/article/index/id/673 我封装的工具类: /** * 圆形进度 * @author chenk ...

  3. Egret的容器--删除对象,遮罩

    class P91F extends egret.Sprite { public constructor() { super(); this.addEventListener(egret.Event. ...

  4. Egret Engine 2D - 遮罩

      矩形遮罩 shp.mask = new egret.Rectangle(20,20,30,50);   注意如果rec发生变化,需要重要将rec赋值给shp.mask 删除遮罩的方法 sprite ...

  5. Egret Engine 2D - 矢量绘图

      绘制矩形 drawRect 绘制矩形边 lineStyle( 10, 0x00ff00 清空绘图 clear 绘制园形 drawCircle 绘制直线 moveTo lineTo 绘制曲线 cur ...

  6. Egret初体验–躲避类小游戏

    下面简单介绍一下我这个游戏:基本上就3个画面(准备再添加一个胜利的界面)开始画面,一个按钮,点击进入游戏游戏画面,滚动的背景,触摸移动的老鹰,从天而降的翔,以及右上角的时间条结束画面,显示结果,关注按 ...

  7. Egret官方案例学习笔记

    1.资源记载方式 (1)Egret引擎是2.0.5. (2)resource/resource.json文件是: { "resources": [ { "name&quo ...

  8. Egret 纹理、计时器

    1. 九宫切 典型例子就是圆角矩形的拉伸问题. 先去P一张绿色的圆角矩形. private createGameScene():void { var box:egret.Bitmap = new eg ...

  9. Egret学习笔记 (Egret打飞机-9.子弹对敌机和主角的碰撞)

    运行起来,虽然主角飞机和敌机都在互相发射子弹,但是子弹打中了就和没打中效果是一样的.. 这一章我们就来处理子弹和飞机的碰撞问题. 我们所有的操作都是基于Main这个容器来做的.所以我就把这个处理放到M ...

随机推荐

  1. u盘启动openwrt

    opkg update opkg install kmod-usb-ohci kmod-usb2 kmod-fs-ext3 opkg install kmod-usb-storage reboot m ...

  2. python面向对象【进阶篇】

    静态方法 通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里可以通过self.调用实例变量或类变量 ...

  3. curl 模拟ajax 请求

    主要是在header请求里加一个  X-Requested-With: XMLHttpRequest curl -v -H "X-Requested-With: XMLHttpRequest ...

  4. js标题文字向上滚动

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  5. 两个有序数组的第n大数

    两个有序数组,各自含有n个元素,求第n大的元素 1.顺序遍历两个数组,计数变量k统计出现的第k小元素,时间复杂度为O(n) 代码例如以下: int getmid(int a[],int b[],int ...

  6. innode 节点

    [root@localhost soft]# ls -i tt1 tt2 xx.c [root@localhost soft]# stat tt1 File: `tt1' Size: 4096 Blo ...

  7. Android之发送短信的两种方式

    SMS涉及的主要类SmsManager 实现SMS主要用到SmsManager类,该类继承自java.lang.Object类,下面我们介绍一下该类的主要成员. 公有方法: ArrayList< ...

  8. 如何解决eclipse上的Android程序“Please ensure that adb is correctly located at 'D:\eclipse\sdk\platform-tools\adb.exe' and can be executed.”小问题?

    首先,把运行的Android模拟器和eclipse一块儿关了, 然后win+R,cmd, 下面输入adb kill_server 再输入adb start_server 之后重新运行项目,不出意外的话 ...

  9. js中的同步与异步

    同步:提交后等待服务器的响应,接收服务器返回的数据后再执行下面的代码    异步:与上面相反,提交后继续执行下面的代码,而在后台继续监听,服务器响应后有程序做相应处理,异步的操作好处是不必等待服务器而 ...

  10. Dhroid框架笔记(IOC、EventBus)

    dhroid 目前包含了6大组件供大家使用1.Ioc容器: (用过spring的都知道)视图注入,对象注入,接口注入,解决类依赖关系2.Eventbus: android平台事件总线框架,独创延时事件 ...