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

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

相关文章

没有特长,凭什么拿高薪?

   今天跟大舅一起散步的时候,说到谁谁谁嫌工资低,大舅说“没有特长,怎么拿的到高工资?”。的确如此,以前做网站挣点钱,当然学这个的动力也是为了挣钱,网上接点小单,挣点小钱,发现多学一门技术...

开始学习IOS

   找了几百G的IOS视频(用不着看完),开始学习IOS(即使电脑还木有),准备学SWIFT,不打算学Objective-C,但已经下好的却是Objective-C的IOS教程,凑合看了一...

一个人的旅行·大理

一个人的旅行·大理

在结束上一份工作后,想着一个人出去走走吧。以前总是抱怨没时间出去,这下有时间了,但只有我一个人。大理 崇圣寺三塔洱海南诏风情岛充满商业气息的古城就不说了。 ...

人像摄影

人像摄影

 上周日早上本来正在家睡觉,看到QQ群里有妹子求约,于是...地点:云大...

测评EVO无人机

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

IOS开发环境配置——黑苹果安装

  最近准备做苹果手机应用开发,不过看了下得在Mac OS 10.10系统下才能做开发,所以有三个选择给我。  一、买台MacBook,我认为暂时没有买的必要,得花万把块钱~等我学会了再买  二、装个...