白鹭引擎(egret)中锚点(anchoroffset)的位置体会
来源:互联网 发布:淘宝卖正版手办的店铺 编辑:程序博客网 时间:2024/04/28 03:40
原文链接
首先关于锚点的作用,我们就以一根线段旋转90度为例:
先绘制一根水平的直线,长度为100
protected startCreateScene(): void { this.drawOneLine(); }protected drawOneLine(){ let ns = new egret.Shape(); ns.x = 100; ns.y = 100; ns.graphics.lineStyle(2,0xFF0000); ns.graphics.moveTo(0,0); ns.graphics.lineTo(100,0); this.addChild(ns);}
现在我们修改下代码,在默认锚点的情况下,旋转90度。为了方便观察,我给线段做了渐变处理,越靠近90度的线段颜色越浅:
protected startCreateScene(): void { this.drawLine(0);}protected drawLine(angle): void { let ns = new egret.Shape(); ns.x = 100; ns.y = 100; ns.graphics.lineStyle(2,0xFF0000); ns.graphics.moveTo(0,0); ns.graphics.lineTo(100,0); ns.alpha = 1-(angle/90)*0.9; ns.rotation = angle; this.addChild(ns); if(angle < 90){ this.drawLine(angle+15); }}
现在我们设置锚点,将中心点居中
protected startCreateScene(): void { this.drawLine(0); this.drawLineCenter(0);}protected drawLineCenter(angle): void { let ns = new egret.Shape(); ns.x = 100; ns.y = 300; ns.anchorOffsetX = 50; // 设置锚点横坐标,位于线段中心 ns.graphics.lineStyle(2,0x000000); ns.graphics.moveTo(0,0); ns.graphics.lineTo(100,0); ns.alpha = 1-(angle/90)*0.9; ns.rotation = angle; this.addChild(ns); if(angle < 90){ this.drawLineCenter(angle+15); }}
关于锚点是如何影响元素旋转的这个示例已经很清楚了。
接下来说这篇文正的关键,上图中有一个不太正常的地方,即黑色线段往左偏移,这个对于刚接触锚点概念的同学来说,无疑是非常残忍的,说白了就是个坑。明明所有元素都放在其应该出现的位置上,并且实现了动效,所有的属性设置也都正确,怎么元素就偏了呢?
现在,我们仍然以长度100的水平线段为例,将锚点依次设置为0,50,100,观察这三根线段的情况,代码如下:
protected startCreateScene(): void { this.drawLineAnchor(100,100,0); this.drawLineAnchor(100,150,50); this.drawLineAnchor(100,200,100);}protected drawLineAnchor(x,y,anchorX):void{ let ns = new egret.Shape(); ns.x = x; ns.y = y; ns.anchorOffsetX = anchorX; ns.graphics.lineStyle(2,0x000000); ns.graphics.moveTo(0,0); ns.graphics.lineTo(100,0); this.addChild(ns);}
从上图中可以很明显的看到锚点对于画面中真实绘制出线段的影响,即元素a在画面中实际的横坐标为x’ = a.x - a.anchorOffsetX。代码在执行到moveTo那一行时,元素根据属性被定位到x=100,y=100后,由于其左上角的锚点被设置为anchorOffsetX=50,anchorOffsetY=0的关系,因此会按照锚点的设置位置向左偏移50再开始绘制初始点。
所以,如果我们必须改变moveTo的初始点和终点,或者改变元素本身的x坐标,才能达到和原线段相同的位置:
protected startCreateScene(): void { this.drawLineAnchor(100, 100, 0); this.drawLineAnchorFixMove(100, 150, 50); // 修改起点终点 this.drawLineAnchorFixX(100, 200, 100); // 修改x坐标}protected drawLineAnchor(){...} // 方法略protected drawLineAnchorFixMove(x, y, anchorX): void { let ns = new egret.Shape(); ns.x = x; ns.y = y; ns.anchorOffsetX = anchorX; ns.graphics.lineStyle(1, 0x000000); ns.graphics.moveTo(0 + anchorX, 0); // 修改线段起始点 ns.graphics.lineTo(100 + anchorX, 0); // 修改线段终点 this.addChild(ns);}protected drawLineAnchorFixX(x, y, anchorX): void { let ns = new egret.Shape(); ns.x = x + anchorX; // 修改整个容器的横坐标 ns.y = y; ns.anchorOffsetX = anchorX; ns.graphics.lineStyle(1, 0x000000); ns.graphics.moveTo(0, 0); ns.graphics.lineTo(100, 0); this.addChild(ns);}
现在大家应该能理解设置锚点之后元素的具体位置了。
阅读全文
0 0
- 白鹭引擎(egret)中锚点(anchoroffset)的位置体会
- Egret 白鹭引擎实践
- Egret---白鹭引擎
- 白鹭引擎(Egret Engine )
- egret白鹭引擎基础介绍
- egret白鹭引擎出现不能操作数组情况的解决方法
- 白鹭(egret)搜素引擎入门
- 简单介绍了解白鹭引擎Egret
- 白鹭引擎Egret在windows下安装
- 白鹭引擎Egret入门知识补充
- egret(白鹭引擎) 学习历程(二)
- Egret白鹭引擎编写影片剪辑出现报错的解决办法
- 游戏引擎 白鹭(egret)学习总结(一)
- Egret引擎的使用
- 白鹭引擎的微信分享接口
- 白鹭引擎-Egret使用一:从创建第一个html5程序到运行,及在centos上运行,发布等
- egret白鹭 基于eui组件的一些动画类 抽屉效果
- poker -3 +白鹭引擎
- java运行时数据区域
- C语言指针传递参数
- 基于mint-ui的移动应用开发案例三(首页)
- HDOJ1002 HOOJ1042大数基本题 C/C++
- 为什么网站不全部用ajax获取内容
- 白鹭引擎(egret)中锚点(anchoroffset)的位置体会
- String源码比较字符串
- 购物车模块设计及实现(SSH架构)
- jquery Ajax提交表单(使用jquery Ajax上传附件)
- 使用word 2013 发布csdn博客
- STL(四) deque 双端队列
- HTTP协议以及HTTP请求中8种请求方法
- 史上最难的一道Java面试题 (分析篇)
- Android解析XML文件的三种方式