理解Cocos2d里的anchorPoint
来源:互联网 发布:round robin调度算法 编辑:程序博客网 时间:2024/05/16 16:06
转载自:http://all-ipad.net/understand-anchorpoint-in-cocos2d/?utm_source=rss
要解释anchorPoint首先需要理解cocos2d里的坐标系统。在cocos2d中有两个坐标系统,一个是touch input使用的,屏幕坐标系统,其原点在左上角,正方向分别朝右和向下。另一个是屏幕绘制用的,使用opengl坐标系统,其原点在左下角,正方向分别朝右和向上。
一般来说,我们在游戏中都使用opengl坐标系统,在处理屏幕点击事件时先要将其转换为opengl坐标系统,使用方法如:
好了,再来看anchorPoint。
在cocos2d中所有从CCNode派生的对象都有anchorPoint属性,cocos2d使用position和anchorPoint两个值来决定在哪里绘制对象,另外在旋转的时候,也会依赖这个属性以决定如何进行旋转。
Positioning
对象的position和anchorPoint默认值都是(0,0),这时对象将绘制在屏幕的左下角,比如下面的代码显示的结果:
如果把anchorPoint发为(-1,-1)之后,结果会是这样:
这张图不是太好理解。
1. anchorPoint的数值单位为是象素单位,而是比例值,取值1表示100%。
2. anchorPoint坐标原点在左下角,(-1,-1)表示在左边一个对象宽度处,下面一个对象高度处。
最后再结合修改position和anchorPoint来看下效果,代码及显示效果如下:
这次把anchorPoint设为(1,1),表示在图像的右上角,同时把position向右上角移动,移动宽度为sprite的width,高度为sprite的height。最终的效果也就相当于把原点设为(0,0)左下角,坐标也设在左下角的位置。
Rotation
一般情况下,如果需要旋转对象,最好将其anchorPoint设为(0.5,0.5),这样一般能够获得我们所期望的旋转效果。但有些时候,也可以通过设置anchorPoint来实现一些特殊效果,比如,把anchorPoint设为(1,1)即右上角,对象将会绕下面的红点旋转:
如果把anchorPoint设为(2,2),一个离对象右上角一个对象远的地方,那么旋转的时候将会像下面这样:
- 理解Cocos2d里的anchorPoint
- Cocos2d中的anchorPoint与position 的理解
- 理解Cocos2d中的anchorPoint与position
- cocos2d-x中的锚点(AnchorPoint)理解
- cocos2d anchorpoint
- 对于anchorPoint的一点理解
- 深入理解Cocos2d-x的anchorPoint锚点和scale缩放之间的配合方式
- 深入理解cocos2d-x的anchorPoint锚点和scale缩放之间的配合方式
- 深入理解Cocos2d-x的anchorPoint锚点和scale缩放之间的配合方式
- 详解Cocos2d-iphone 的position和anchorPoint
- 彻底理解CALayer的position与anchorPoint
- cocos2d中的anchorPoint
- cocos2d中的anchorPoint
- cocos2d中的anchorPoint
- cocos2d中的anchorPoint
- cocos2d中的anchorPoint
- Cocos2d-x中的AnchorPoint
- 【cocos2d-x】AnchorPoint
- How to stream video to your android phone 如何将视频远程传输至你的android手机?
- swap的真正作用
- 我想以后记着你心里都是你,再去爱别人。
- JMeter入门6---分布执行
- JSON对象与JSON字符串相互转换
- 理解Cocos2d里的anchorPoint
- 一个tomcat下两个项目(ZK项目)共享session
- 圆的计算
- android中的AdapterView阅读
- Linux内核启动流程分析
- VIM编辑和查看二进制文件
- dereferencing pointer to incomplete type 错误
- 你要离开我不会挽留,我会给你我仅有的,放你自由
- Xcode:用bash统计代码行数