当前位置:首页 > 未命名 > 正文内容

Egret教程 egret.Shape 显示对象绘制图形

8年前 (2018-05-22)未命名
private drawGrid() { // 绘制
    //指定填充色开始填充
    this.graphics.beginFill(0x000ff);
    //绘制矩形,指定位置和宽高
    this.graphics.drawRect(0, 0, 50, 50);
    //结束填充
    this.graphics.endFill();
    this.graphics.beginFill(0x000ff);
    this.graphics.drawRect(50, 50, 50, 50);
    this.graphics.endFill();
    this.graphics.beginFill(0xff0000);
    this.graphics.drawRect(50, 0, 50, 50);
    this.graphics.endFill();
    this.graphics.beginFill(0xff0000);
    this.graphics.drawRect(0, 50, 50, 50);
    this.graphics.endFill();
}
private onAddToStage(event: egret.Event) {
		var shp: egret.Shape = new egret.Shape();
		shp.graphics.beginFill(0x00ff00);
		shp.graphics.drawRect(0, 0, 100, 100);
		shp.graphics.endFill();

		//改变矩形的位置(左上角顶点)
		shp.x = 100;
		shp.y = 100;

		//修改锚点的位置
		shp.anchorOffsetX = 50;
		shp.anchorOffsetY = 50;
		this.addChild(shp);
}

//通过触摸移动显示对象
	private moveObjectByTouch(): void {
		var draggedObject: egret.Shape;
		var offsetX: number;
		var offsetY: number;

		var circle: egret.Shape = new egret.Shape();
		circle.graphics.beginFill(0xff0000);
		circle.graphics.drawCircle(25, 25, 25);
		circle.graphics.endFill();
		this.addChild(circle);

		circle.touchEnabled = true;
		// circle.addEventListener(egret.TouchEvent.TOUCH_BEGIN, startMove, this);
		circle.addEventListener(egret.TouchEvent.TOUCH_BEGIN, (e: egret.TouchEvent) => {
			//记录手指按到的对象
			draggedObject = e.currentTarget;
			offsetX = e.stageX - circle.x;
			offsetY = e.stageY - circle.y;
			//将触摸的对象置于容器顶层
			this.addChild(draggedObject);
			this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, onMove, this);
		}, this);
		circle.addEventListener(egret.TouchEvent.TOUCH_END, stopMove, this);

		function stopMove(e: egret.TouchEvent): void {
			console.log("stopMove");
			this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, onMove, this);
		}
		function onMove(e: egret.TouchEvent): void {
			draggedObject.x = e.stageX - offsetX;
			draggedObject.y = e.stageY - offsetY;
		}
	}


扫描二维码推送至手机访问。

版权声明:本文由小祥子的博客发布,如需转载请注明出处。

本文地址:http://www.xiaoxiangzi.com/post/72.html

相关文章

VB做的一个自动获取最新任务的小程序

VB做的一个自动获取最新任务的小程序

以前就爱使用Zblog,如今一看,居然有PHP的版本,果断下载了一个来使用。平时没事的时候,可以写写博文,或者记录一些好用的代码。最近学了PHP与VB,并且都开发了点小程序出来,当然,必须得实用或者能...

OC时间戳转时间方法系列

// 时间戳转时间 + (NSString *)timeStampConv:(NSString *)timestamp; // 秒转换成00:00...

新上线一款工具——google搜索

新上线一款工具——google搜索

 由于Google被国内封了,所以做了个google搜索结果调用的程序。 地址:http://www.xiaoxiangzi.com/tool/google.h...

人像摄影

人像摄影

12.25地点:翠湖时间:2014年12月25日  地点:云大时间:2014年12月28日...

测评EVO无人机

在本地的无人机群里了解到EVO无人机,于是在淘宝里买了一台回来玩,但收到货后发现跟想象的不一样。关于APP与遥控器首先在APPStore中没有找到这款无人机的APP,国内各大安卓市场也没有找到,最后在...

时隔两年,重启Blog

为了用这个域名搞微信相关的东西,网站关闭了很久,这次把网站又重新假设了起来。上一次发文,我刚入门iOS,这一次发文,我已经是一个已有2年iOS开发经验的程序员了,不由感叹,时间过的真快。看着之前的一篇...