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

cocos2dx封装一个具有Layout功能的Point类 (提供源码),第1张

概述(原创文章,转载请注明原文出处:http://blog.csdn.net/while0/article/details/79032004) 基于cocos2dx开发游戏,免不了设置节点或精灵的位置,这些位置坐标常常不是一个绝对坐标值,而是相对于其它节点的相对坐标。例如:精灵A与精灵B左对齐,精灵A与精灵B中心对齐等等。 计算这些相对坐标值,每次都需要进行计算,计算时要考虑到精灵的anchorPoi

(原创文章,转载请注明原文出处: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,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,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,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,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,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,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,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,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,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,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,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,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,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,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,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,xnum*yNum*sizeof(GmbsGrID*));    }    GmbsGrID(Node* ownerNode,Rect& rect,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,xnum*yNum*sizeof(GmbsGrID*));    }    GmbsGrID(Rect& rect,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,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 down    GmbsGrID& 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,origin.y + rect.size.height*yNew);    		m_children[yNew*m_xnum + x] = new GmbsGrID(m_ownerNode,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类 (提供源码)全部内容,希望文章能够帮你解决cocos2dx封装一个具有Layout功能的Point类 (提供源码)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1085353.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存