(原创文章,转载请注明原文出处:http://blog.csdn.net/while0/article/details/79032004)

基于cocos2dx开发游戏,免不了设置节点或精灵的位置,这些位置坐标常常不是一个绝对坐标值,而是相对于其它节点的相对坐标。例如:精灵A与精灵B左对齐,精灵A与精灵B中心对齐等等。

计算这些相对坐标值,每次都需要进行计算,计算时要考虑到精灵的anchorPoint, scale等,比较繁琐,一不留神就搞错了,调试来调试去浪费时间。本文封装了一个很方便的工具类,帮助你计算这些相对坐标。

直接上源码:

GmbsPoint.h (无cpp文件)

#ifndef __GMBSPOINT_H__
#define __GMBSPOINT_H__#include "cocos2d.h"
#include "GmbsGrid.h"NS_CC_BEGINclass GmbsPoint : public Point
{
public:Node *m_targetNode;GmbsPoint(Node* targetNode = NULL){m_targetNode = targetNode;if (targetNode){Point pt = targetNode->getPosition();x = pt.x;y = pt.y;}}GmbsPoint(Node* targetNode, float xx, float yy){m_targetNode = targetNode;x = xx;y = yy;}GmbsPoint& reset(Node* targetNode, float xx = 0, float yy = 0){m_targetNode = targetNode;x = xx;y = yy;return *this;}public:GmbsPoint& leftAlign(Node* baseNode, float leftPadding = 0){Point baseAnchorPoint(0, 0);Point basePoint = baseNode->getPosition();Node* baseParent = baseNode->getParent();if (baseParent != NULL)basePoint = baseParent->convertToWorldSpace(basePoint);if (!baseNode->isIgnoreAnchorPointForPosition())baseAnchorPoint = baseNode->getAnchorPoint();basePoint.x -= baseNode->getContentSize().width * baseAnchorPoint.x * getScaleX(baseNode);Point point(basePoint.x + leftPadding, basePoint.y);Point anchorPoint(0, 0);if (!m_targetNode->isIgnoreAnchorPointForPosition())anchorPoint = m_targetNode->getAnchorPoint();point.x += m_targetNode->getContentSize().width * anchorPoint.x * getScaleX(m_targetNode);point = m_targetNode->getParent()->convertToNodeSpace(point);x = point.x;return *this;}GmbsPoint& leftAlign(GmbsGrid& baseGrid, float leftPadding = 0){return leftAlign(baseGrid.m_ownerNode, leftPadding + baseGrid.origin.x);}GmbsPoint& rightAlign(Node* baseNode, float rightPadding = 0){Point baseAnchorPoint(0, 0);Point basePoint = baseNode->getPosition();Node* baseParent = baseNode->getParent();if (baseParent != NULL)basePoint = baseParent->convertToWorldSpace(basePoint);if (!baseNode->isIgnoreAnchorPointForPosition())baseAnchorPoint = baseNode->getAnchorPoint();basePoint.x += baseNode->getContentSize().width * (1 - baseAnchorPoint.x) * getScaleX(baseNode);Point point(basePoint.x - rightPadding, basePoint.y);Point anchorPoint(0, 0);if (!m_targetNode->isIgnoreAnchorPointForPosition())anchorPoint = m_targetNode->getAnchorPoint();point.x -= m_targetNode->getContentSize().width * (1 - anchorPoint.x) * getScaleX(m_targetNode);point = m_targetNode->getParent()->convertToNodeSpace(point);x = point.x;return *this;}GmbsPoint& rightAlign(GmbsGrid& baseGrid, float rightPadding = 0){Node* baseNode = baseGrid.m_ownerNode;Point baseAnchorPoint(0, 0);Point basePoint = baseNode->getPosition();Node* baseParent = baseNode->getParent();if (baseParent != NULL)basePoint = baseParent->convertToWorldSpace(basePoint);if (!baseNode->isIgnoreAnchorPointForPosition())baseAnchorPoint = baseNode->getAnchorPoint();basePoint.x += baseGrid.origin.x + baseGrid.size.width - baseNode->getContentSize().width * baseAnchorPoint.x * getScaleX(baseNode);Point point(basePoint.x - rightPadding, basePoint.y);Point anchorPoint(0, 0);if (!m_targetNode->isIgnoreAnchorPointForPosition())anchorPoint = m_targetNode->getAnchorPoint();point.x -= m_targetNode->getContentSize().width * (1 - anchorPoint.x) * getScaleX(m_targetNode);point = m_targetNode->getParent()->convertToNodeSpace(point);x = point.x;return *this;}GmbsPoint& topAlign(Node* baseNode, float topPadding = 0){Point baseAnchorPoint(0, 0);Point basePoint = baseNode->getPosition();Node* baseParent = baseNode->getParent();if (baseParent != NULL)basePoint = baseParent->convertToWorldSpace(basePoint);if (!baseNode->isIgnoreAnchorPointForPosition())baseAnchorPoint = baseNode->getAnchorPoint();basePoint.y += baseNode->getContentSize().height * (1 - baseAnchorPoint.y) * getScaleY(baseNode);Point point(basePoint.x, basePoint.y - topPadding);Point anchorPoint(0, 0);if (!m_targetNode->isIgnoreAnchorPointForPosition())anchorPoint = m_targetNode->getAnchorPoint();point.y -= m_targetNode->getContentSize().height * (1 - anchorPoint.y) * getScaleY(m_targetNode);point = m_targetNode->getParent()->convertToNodeSpace(point);y = point.y;return *this;}GmbsPoint& topAlign(GmbsGrid& baseGrid, float topPadding = 0){Node* baseNode = baseGrid.m_ownerNode;Point baseAnchorPoint(0, 0);Point basePoint = baseNode->getPosition();Node* baseParent = baseNode->getParent();if (baseParent != NULL)basePoint = baseParent->convertToWorldSpace(basePoint);if (!baseNode->isIgnoreAnchorPointForPosition())baseAnchorPoint = baseNode->getAnchorPoint();basePoint.y += baseGrid.origin.y + baseGrid.size.height - baseNode->getContentSize().height * baseAnchorPoint.y * getScaleY(baseNode);Point point(basePoint.x, basePoint.y - topPadding);Point anchorPoint(0, 0);if (!m_targetNode->isIgnoreAnchorPointForPosition())anchorPoint = m_targetNode->getAnchorPoint();point.y -= m_targetNode->getContentSize().height * (1 - anchorPoint.y) * getScaleY(m_targetNode);point = m_targetNode->getParent()->convertToNodeSpace(point);y = point.y;return *this;}GmbsPoint& bottomAlign(Node* baseNode, float bottomPadding = 0){Point baseAnchorPoint(0, 0);Point basePoint = baseNode->getPosition();Node* baseParent = baseNode->getParent();if (baseParent != NULL)basePoint = baseParent->convertToWorldSpace(basePoint);if (!baseNode->isIgnoreAnchorPointForPosition())baseAnchorPoint = baseNode->getAnchorPoint();basePoint.y -= baseNode->getContentSize().height * baseAnchorPoint.y * getScaleY(baseNode);Point point(basePoint.x, basePoint.y + bottomPadding);Point anchorPoint(0, 0);if (!m_targetNode->isIgnoreAnchorPointForPosition())anchorPoint = m_targetNode->getAnchorPoint();point.y += m_targetNode->getContentSize().height * anchorPoint.y * getScaleY(m_targetNode);point = m_targetNode->getParent()->convertToNodeSpace(point);y = point.y;return *this;}GmbsPoint& bottomAlign(GmbsGrid& baseGrid, float bottomPadding = 0){return bottomAlign(baseGrid.m_ownerNode, bottomPadding + baseGrid.origin.y);}GmbsPoint& xMiddleAlign(Node* baseNode, float padding = 0){Point baseAnchorPoint(0, 0);Point basePoint = baseNode->getPosition();Node* baseParent = baseNode->getParent();if (baseParent != NULL)basePoint = baseParent->convertToWorldSpace(basePoint);if (!baseNode->isIgnoreAnchorPointForPosition())baseAnchorPoint = baseNode->getAnchorPoint();basePoint.x += baseNode->getContentSize().width * (0.5 - baseAnchorPoint.x) * getScaleX(baseNode);Point point(basePoint.x + padding, basePoint.y);Point anchorPoint(0, 0);if (!m_targetNode->isIgnoreAnchorPointForPosition())anchorPoint = m_targetNode->getAnchorPoint();point.x -= m_targetNode->getContentSize().width * (0.5 - anchorPoint.x) * getScaleX(m_targetNode);point = m_targetNode->getParent()->convertToNodeSpace(point);x = point.x;return *this;}GmbsPoint& xMiddleAlign(GmbsGrid& baseGrid, float padding = 0){Node* baseNode = baseGrid.m_ownerNode;Point baseAnchorPoint(0, 0);Point basePoint = baseNode->getPosition();Node* baseParent = baseNode->getParent();if (baseParent != NULL)basePoint = baseParent->convertToWorldSpace(basePoint);if (!baseNode->isIgnoreAnchorPointForPosition())baseAnchorPoint = baseNode->getAnchorPoint();basePoint.x += baseGrid.origin.x + baseGrid.size.width/2 - baseNode->getContentSize().width * baseAnchorPoint.x * getScaleX(baseNode);Point point(basePoint.x + padding, basePoint.y);Point anchorPoint(0, 0);if (!m_targetNode->isIgnoreAnchorPointForPosition())anchorPoint = m_targetNode->getAnchorPoint();point.x -= m_targetNode->getContentSize().width * (0.5 - anchorPoint.x) * getScaleX(m_targetNode);point = m_targetNode->getParent()->convertToNodeSpace(point);x = point.x;return *this;}GmbsPoint& yMiddleAlign(Node* baseNode, float padding = 0){Point baseAnchorPoint(0, 0);Point basePoint = baseNode->getPosition();Node* baseParent = baseNode->getParent();if (baseParent != NULL)basePoint = baseParent->convertToWorldSpace(basePoint);if (!baseNode->isIgnoreAnchorPointForPosition())baseAnchorPoint = baseNode->getAnchorPoint();basePoint.y += baseNode->getContentSize().height * (0.5 - baseAnchorPoint.y) * getScaleY(baseNode);Point point(basePoint.x, basePoint.y + padding);Point anchorPoint(0, 0);if (!m_targetNode->isIgnoreAnchorPointForPosition())anchorPoint = m_targetNode->getAnchorPoint();point.y -= m_targetNode->getContentSize().height * (0.5 - anchorPoint.y) * getScaleY(m_targetNode);point = m_targetNode->getParent()->convertToNodeSpace(point);y = point.y;return *this;}GmbsPoint& yMiddleAlign(GmbsGrid& baseGrid, float padding = 0){Node* baseNode = baseGrid.m_ownerNode;Point baseAnchorPoint(0, 0);Point basePoint = baseNode->getPosition();Node* baseParent = baseNode->getParent();if (baseParent != NULL)basePoint = baseParent->convertToWorldSpace(basePoint);if (!baseNode->isIgnoreAnchorPointForPosition())baseAnchorPoint = baseNode->getAnchorPoint();basePoint.y += baseGrid.origin.y + baseGrid.size.height/2 - baseNode->getContentSize().height * baseAnchorPoint.y * getScaleY(baseNode);Point point(basePoint.x, basePoint.y + padding);Point anchorPoint(0, 0);if (!m_targetNode->isIgnoreAnchorPointForPosition())anchorPoint = m_targetNode->getAnchorPoint();point.y -= m_targetNode->getContentSize().height * (0.5 - anchorPoint.y) * getScaleY(m_targetNode);point = m_targetNode->getParent()->convertToNodeSpace(point);y = point.y;return *this;}GmbsPoint& rightTo(Node* baseNode, float rightPadding = 0){Point baseAnchorPoint(0, 0);Point basePoint = baseNode->getPosition();Node* baseParent = baseNode->getParent();if (baseParent != NULL)basePoint = baseParent->convertToWorldSpace(basePoint);if (!baseNode->isIgnoreAnchorPointForPosition())baseAnchorPoint = baseNode->getAnchorPoint();basePoint.x -= baseNode->getContentSize().width * baseAnchorPoint.x * getScaleX(baseNode);Point point(basePoint.x - rightPadding, basePoint.y);Point anchorPoint(0, 0);if (!m_targetNode->isIgnoreAnchorPointForPosition())anchorPoint = m_targetNode->getAnchorPoint();point.x -= m_targetNode->getContentSize().width * (1 - anchorPoint.x) * getScaleX(m_targetNode);;point = m_targetNode->getParent()->convertToNodeSpace(point);x = point.x;return *this;}GmbsPoint& leftTo(Node* baseNode, float leftPadding = 0){Point baseAnchorPoint(0, 0);Point basePoint = baseNode->getPosition();Node* baseParent = baseNode->getParent();if (baseParent != NULL)basePoint = baseParent->convertToWorldSpace(basePoint);if (!baseNode->isIgnoreAnchorPointForPosition())baseAnchorPoint = baseNode->getAnchorPoint();basePoint.x += baseNode->getContentSize().width * (1 - baseAnchorPoint.x) * getScaleX(baseNode);Point point(basePoint.x + leftPadding, basePoint.y);Point anchorPoint(0, 0);if (!m_targetNode->isIgnoreAnchorPointForPosition())anchorPoint = m_targetNode->getAnchorPoint();point.x += m_targetNode->getContentSize().width * anchorPoint.x * getScaleX(m_targetNode);;point = m_targetNode->getParent()->convertToNodeSpace(point);x = point.x;return *this;}GmbsPoint& bottomTo(Node* baseNode, float bottomPadding = 0){Point baseAnchorPoint(0, 0);Point basePoint = baseNode->getPosition();Node* baseParent = baseNode->getParent();if (baseParent != NULL)basePoint = baseParent->convertToWorldSpace(basePoint);if (!baseNode->isIgnoreAnchorPointForPosition())baseAnchorPoint = baseNode->getAnchorPoint();basePoint.y += baseNode->getContentSize().height * (1 - baseAnchorPoint.y) * getScaleY(baseNode);Point point(basePoint.x, basePoint.y + bottomPadding);Point anchorPoint(0, 0);if (!m_targetNode->isIgnoreAnchorPointForPosition())anchorPoint = m_targetNode->getAnchorPoint();point.y += m_targetNode->getContentSize().height * anchorPoint.y * getScaleY(m_targetNode);;point = m_targetNode->getParent()->convertToNodeSpace(point);y = point.y;return *this;}GmbsPoint& topTo(Node* baseNode, float topPadding = 0){Point baseAnchorPoint(0, 0);Point basePoint = baseNode->getPosition();Node* baseParent = baseNode->getParent();if (baseParent != NULL)basePoint = baseParent->convertToWorldSpace(basePoint);if (!baseNode->isIgnoreAnchorPointForPosition())baseAnchorPoint = baseNode->getAnchorPoint();basePoint.y -= baseNode->getContentSize().height * baseAnchorPoint.y * getScaleY(baseNode);;Point point(basePoint.x, basePoint.y - topPadding);Point anchorPoint(0, 0);if (!m_targetNode->isIgnoreAnchorPointForPosition())anchorPoint = m_targetNode->getAnchorPoint();point.y -= m_targetNode->getContentSize().height * (1 - anchorPoint.y) * getScaleY(m_targetNode);;;point = m_targetNode->getParent()->convertToNodeSpace(point);y = point.y;return *this;}static float getScaleX(Node* node){float scale = node->getScaleX();Node* parent = node->getParent();while (parent != NULL){scale *= parent->getScaleX();parent = parent->getParent();}return scale;}static float getScaleY(Node* node){float scale = node->getScaleY();Node* parent = node->getParent();while (parent != NULL){scale *= parent->getScaleY();parent = parent->getParent();}return scale;}
};NS_CC_END#endif

GmbsGrid.h (无cpp文件)

#ifndef __GMBSGRID_H__
#define __GMBSGRID_H__#include "cocos2d.h"NS_CC_BEGINclass GmbsGrid : public Rect
{
protected:int m_xNum;int m_yNum;GmbsGrid** m_children;public:Node *m_ownerNode;GmbsGrid(Node* ownerNode, int xNum, int yNum){m_ownerNode = ownerNode;origin = Point(0, 0);size = ownerNode->getContentSize();m_xNum = xNum;m_yNum = yNum;m_children = (GmbsGrid**)malloc(xNum*yNum*sizeof(GmbsGrid*));memset(m_children, 0, xNum*yNum*sizeof(GmbsGrid*));}GmbsGrid(Node* ownerNode, Rect& rect, int xNum, int yNum){m_ownerNode = ownerNode;origin = rect.origin;size = rect.size;m_xNum = xNum;m_yNum = yNum;m_children = (GmbsGrid**)malloc(xNum*yNum*sizeof(GmbsGrid*));memset(m_children, 0, xNum*yNum*sizeof(GmbsGrid*));}GmbsGrid(Rect& rect, int xNum, int yNum){m_ownerNode = NULL;origin = rect.origin;size = rect.size;m_xNum = xNum;m_yNum = yNum;m_children = (GmbsGrid**)malloc(xNum*yNum*sizeof(GmbsGrid*));memset(m_children, 0, xNum*yNum*sizeof(GmbsGrid*));}virtual ~GmbsGrid(){release();}void release(){for (int j = 0; j < m_yNum; j++){for (int i = 0; i < m_xNum; i++){if (m_children[j*m_xNum + i] != NULL){delete m_children[j*m_xNum + i];m_children[j*m_xNum + i] = NULL;}}}}GmbsGrid& child(int x, int y){if (m_children[y*m_xNum + x] == NULL){Rect rect;rect.size.setSize(size.width/m_xNum, size.height/m_yNum);rect.origin.setPoint(origin.x + rect.size.width*x, origin.y + rect.size.height*y);m_children[y*m_xNum + x] = new GmbsGrid(m_ownerNode, rect, 1, 1);}return *m_children[y*m_xNum + x];}//counting from up to downGmbsGrid& childUtd(int x, int y){int yNew = m_yNum - 1 - y;if (m_children[yNew*m_xNum + x] == NULL){Rect rect;rect.size.setSize(size.width/m_xNum, size.height/m_yNum);rect.origin.setPoint(origin.x + rect.size.width*x, origin.y + rect.size.height*yNew);m_children[yNew*m_xNum + x] = new GmbsGrid(m_ownerNode, rect, 1, 1);}return *m_children[yNew*m_xNum + x];}void setOwnerNode(Node* ownerNode){m_ownerNode = ownerNode;}
};NS_CC_END#endif

举例:

1) spriteA与spriteB中心对齐:

GmbsPoint pt(spriteA);
pt.xMiddleAlign(spriteB).yMiddleAlign(spriteB);
spriteA->setPosition(pt);

2) spriteA与spriteB左对齐且底对齐:

GmbsPoint pt(spriteA);
pt.leftAlign(spriteB).bottomAlign(spriteB);
spriteA->setPosition(pt);

3) spriteA在spriteB左侧,且相距间隔为10,它们底部对齐:

GmbsPoint pt(spriteA);
pt.leftTo(spriteB, 10).bottomAlign(spriteB);
spriteA->setPosition(pt);

cocos2dx封装一个具有Layout功能的Point类 (提供源码)相关推荐

  1. 微信/易信公共平台开发(一):开发了一个简单易用的PHP类(提供源码),十几行代码搞定一个公众号

    这两天学习了一下微信公共平台和易信公共平台的开发 (易信与微信协议是基本一样的)(PS:没听说过易信?你out了?) 公共平台开发原理这里就不讲了,如果是初次接触的同学,建议先看 David_Tang ...

  2. java实现简易聊天窗口先运行服务器还是客户端_一个简易聊天功能的服务器端和客户端源码...

    学习完J2SE可以写一个简易的聊天软件来让刚学的知识融会贯通,代码注释的很详细哦! 开发版本历程: V0.1:客户端实现一个界面 V0.2:客户端界面有输入框和显示框的界面 V0.3:客户端关闭按钮可 ...

  3. 独家强大情侣头像网名个性签名多功能工具微信小程序源码下载

    强大的多功能情侣小程序源码 内含N款功能,每一款都是独特的 列如的功能有: 情侣头像(这个大家都知道哈,就不用我介绍了) 情侣网名(这个也就不用我介绍了看名字就知道了 网名生成(根据你输入的文字生成网 ...

  4. 精美UI强大娱乐功能组合微信小程序源码

    介绍: 这是一个多娱乐功能的小程序 具体由以下功能组合: 网易云在线音乐(***音乐和网易云功能界面一样) 外卖CPS(外卖平台优惠劵) 打车CPS(打车平台优惠劵) 头像功能(多分类头像,另外还支持 ...

  5. 【小程序源码】精美UI强大娱乐功能组合微信小程序源码下载,安装简单

    这是一个多娱乐功能的小程序 具体由以下功能组合: 在线音乐 短视频去印 外卖CPS(外卖平台优惠劵) 打车CPS(打车平台优惠劵) 头像功能(多分类头像,另外还支持姓氏头像制作) 图片加水印 表情包功 ...

  6. 【小程序源码】升级版王者荣耀铭文多功能助手微信小程序源码下载

    这是一个王者铭文小程序 支持每一个英雄的铭文出装推荐查看 支持铭文组合模拟数据 另外还支持游戏重复名生成和空白名生成 比之前分享的一款单一铭文好一点吧 废话不多说,下面就一起来看看小编的测试演示图吧! ...

  7. 升级版王者荣耀铭文多功能助手微信小程序源码下载-支持多种流量主

    这是一个王者铭文小程序 支持每一个英雄的铭文出装推荐查看 支持铭文组合模拟数据 另外还支持游戏重复名生成和空白名生成 比之前分享的一款单一铭文好一点吧 另外该款小程序还支持多种流量主模式 比如:激励视 ...

  8. C++求一个整数的各位数字总和(附完整源码)

    C++求一个整数的各位数字总和算法 C++求一个整数的各位数字总和算法完整源码(定义,实现,main函数测试) C++求一个整数的各位数字总和算法完整源码(定义,实现,main函数测试) int su ...

  9. 看到了一个 蒙特卡洛方法 随机数得出 圆周率的c++ 源码

    package bt6;import java.util.Random; /*** 看到了一个 蒙特卡洛方法 随机数得出 圆周率的c++ 源码 ,复制过来 一个Java版的见笑了* @author s ...

最新文章

  1. How can I pretty-print JSON in python?
  2. Linux中文件系统简介
  3. 小学计算机国培研修总结,小学数学国培研修总结
  4. VSCODE更改文件时,提示:EACCES: permission denied的解决办法(ubuntu16.04虚拟机)
  5. Mysql安装两种方法
  6. Java对象内存图一
  7. (数据库系统概论|王珊)第十一章并发控制-第二、三、四节:封锁、封锁协议活锁和死锁
  8. 苹果保修期_iPhone 保修期内哪些情况可以获得免费维修?
  9. python dll文件丢失_python34.dll
  10. 批处理 备份网络数据
  11. 转发:关于数据权限设计的思考
  12. Python3笔记——IDE的选择
  13. xcode5切换IOS7,IOS6,IOS5模拟器
  14. 不用任何插件:小白如何白嫖百度网盘下载速度(不好用来打我,嘿嘿)
  15. RFC 2544 标准—以太网测试仪
  16. 红孩儿编辑器的模块设计8
  17. Android 获取指南针数据
  18. 36篇精品文章搞定所有TOEIC单词
  19. JavaScript基础知识快速预览
  20. 使用alwayson后如何收缩数据库日志

热门文章

  1. linux系统最小化快捷键,Linux系统最全的控制台快捷键
  2. 计算页面停留时长的另类方式
  3. activate tensorflow 激活失败解决方案
  4. onmouseover阻止冒泡
  5. DNS预解析prefetch
  6. 计算机电源认证,80PLUS认证等级及与普通电源区别
  7. 如何利用k线图做技术分析
  8. 专业知识和计算机思维的关系是什么意思,什么是计算机思维?
  9. #微软学生开发者峰会
  10. 自定义LinearLayout