转自:http://blog.csdn.net/cjopengler/article/details/7045638

anchor point 究竟是怎么回事? 之所以造成不容易理解的是因为我们平时看待一个图片是 以图片的中心点 这一个维度来决定图片的位置的。而在cocos2d中决定一个 图片的位置是由两个维度 一个是 position 也就是图片的中心点 另外一个是anchor point。只要我们搞清楚他们的关系,自然就迎刃而解。

他们的关系是这样的:

actualPosition.x = position.x + width*(0.5 - anchor_point.x);
acturalPosition.y = position.y + height*(0.5 - anchor_point.y);
//actualPosition 是sprite实际上在屏幕显示的位置, poistion是 程序设置的, achor_point也是程序设置的。

具体看下面的例子一:

CCSprite *sprite = [CCSprite spritewithFile:@"blackSquare.png"];
sprite.position=ccp(,);
sprite.anchorPoint=ccp(,);
[self addChild:sprite];

具体效果如下:

根据上面的公式: 假设精灵的width = height = 10.

actualPosition.x =  + *(0.5 - ) = ;

actualPosition.y  =  + *(0.5 - ) = ; 

(5, 5) 这个结果正是现在图片的在屏幕上的实际位置。

CCSprite *sprite = [CCSprite spritewithFile:@"blackSquare.png"];
sprite.position=ccp(,);
sprite.anchorPoint=ccp(-,-);
[self addChild:sprite];

具体效果如下:

根据上面的公式: 假设精灵的width = height = 10.

actualPosition.x =  + *(0.5 - (-)) = ;

actualPosition.y  =  + *(0.5 - (-)) = ; 

(15, 15) 这个结果正是现在图片的在屏幕上的实际位置。

例子三

CCSprite *sprite = [CCSprite spritewithFile:@"blackSquare.png"];
sprite.anchorPoint=ccp(,);
sprite.position=ccp(sprite.contentSize.width , sprite.contentSize.height);
[self addChild:sprite];

根据上面的公式: 假设精灵的width = height = 10.

actualPosition.x =  + *(0.5 - ()) = ;

actualPosition.y  =  + *(0.5 - ()) = ; 

(5, 5) 这个结果正是现在图片的在屏幕上的实际位置。

最新文章

  1. 北京电子科技学院(BESTI)实验报告4
  2. 控件UI性能调优 -- SizeChanged不是万能的
  3. ubuntu安装WPS
  4. VMnet1和V8
  5. 【Swing】理解Swing中的事件与线程
  6. Android之NDK开发
  7. Every student in every school should have the opportunity to learn to code
  8. DWZ(JUI) 教程 左侧栏默认是关闭状态的问题
  9. HDU 4405-Aeroplane chess(概率dp)
  10. java之内部类与匿名内部类
  11. UVa 725暴力求解
  12. Python中def及lambda的功能介绍
  13. 汇编实验一 查看CPU和内存,用机器指令和汇编语言指令编程
  14. CentOS 7下Samba服务安装与配置详解
  15. CDI
  16. SSO 单点登录
  17. 理解git的分支原理,更好地使用git
  18. Scala详解---------数组、元组、映射
  19. Specified VM install not found: type Standard VM, name jdk1.7
  20. Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile)

热门文章

  1. RhinoMock异常ExpectationViolationException以及解决
  2. Understanding The Linux Virtual Memory Manager
  3. EXCEL多列名称组合成一列名称
  4. gulp入门与一些基本设置
  5. pssh安装和使用
  6. Makefile学习之路——3
  7. java Map & List 遍历
  8. ios UIImageView异步加载网络图片2
  9. css让footer永远保持在页面底部
  10. [转]关闭word进程