[转]anchorPoint 锚点解析
2024-10-16 09:39:28
转自: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) 这个结果正是现在图片的在屏幕上的实际位置。
最新文章
- 北京电子科技学院(BESTI)实验报告4
- 控件UI性能调优 -- SizeChanged不是万能的
- ubuntu安装WPS
- VMnet1和V8
- 【Swing】理解Swing中的事件与线程
- Android之NDK开发
- Every student in every school should have the opportunity to learn to code
- DWZ(JUI) 教程 左侧栏默认是关闭状态的问题
- HDU 4405-Aeroplane chess(概率dp)
- java之内部类与匿名内部类
- UVa 725暴力求解
- Python中def及lambda的功能介绍
- 汇编实验一 查看CPU和内存,用机器指令和汇编语言指令编程
- CentOS 7下Samba服务安装与配置详解
- CDI
- SSO 单点登录
- 理解git的分支原理,更好地使用git
- Scala详解---------数组、元组、映射
- Specified VM install not found: type Standard VM, name jdk1.7
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile)