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

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

7年前 (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

相关文章

发现个大BUG,结果...我错了

   首先,有个好消息就是我之前考的《网络通信安全管理员》通过了,有了它,公司的经营性ICP备案就有着落了。鼓掌,啪啪啪~另外,由于我打算学习IOS,所以准备新建一个IOS栏目,作为我的学习...

OC时间戳转时间方法系列

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

滇池红嘴鸥

滇池红嘴鸥

 拍摄时间:2014年11月8日拍摄地点:昆明·海埂大坝使用器材:佳能70D + 55-250mm...

时隔两年,重启Blog

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

开始学习IOS

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

Objective-C修改图片尺寸/生成缩略图

+(UIImage *)imageResize :(UIImage*)img andResizeTo:(CGSize)newSize {   &n...