(原创文章,转载请注明原文出处: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_BEGIN

class 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_BEGIN

class 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类 (提供源码)的更多相关文章

  1. HTML实现代码雨源码及效果示例

    这篇文章主要介绍了HTML实现代码雨源码及效果示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  2. 利用Node实现HTML5离线存储的方法

    这篇文章主要介绍了利用Node实现HTML5离线存储的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  3. ios – 使用带有NodeJs HTTPS的certificates.cer

    我为IOS推送通知生成了一个.cer文件,我希望将它与NodeJSHTTPS模块一起使用.我发现HTTPS模块的唯一例子是使用.pem和.sfx文件,而不是.cer:有解决方案吗解决方法.cer文件可以使用两种不同的格式进行编码:PEM和DER.如果您的文件使用PEM格式编码,您可以像使用任何其他.pem文件一样使用它(有关详细信息,请参见Node.jsdocumentation):如果您的文件使

  4. 如何在XCode IDE中构建NodeJS?

    如何在XCodeIDE中将NodeJS构建为项目?NodeJS构建指令说它应该用以下内容构建:但是我希望在XCodeIDE中构建.我真正想要做的是在我的应用程序中嵌入NodeJS,所以我想如果我可以在XCode中构建NodeJS,那么我可以调整它以在我建立和运行NodeJS后添加我的应用程序.我想通过让V8在XCode中编译来取得一些进展,现在我正在尝试将NodeJS添加到V8项目中.解决方法在节点存储库根目录中运行./configure–xcode,您将获得所需的node.xcodeproj文件.

  5. 源码推荐:简化Swift编写的iOS动画,iOS Material Design库

    本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  6. 深入云存储系统Swift核心组件:Ring实现原理剖析

    它的目的是用于托管Rackspace的CloudFilesservice,原始项目代号是swift,所以沿用至今。Ring是Swift中最重要的组件,用于记录存储对象与物理位置间映射关系。先来看一下Swift文档中关于Ring的描述:Ring用来确定数据驻留在集群中的位置。有单独对应于Account数据库、container数据库和单个object的ring。Ring使用zone的概念来保证数据的隔离。每个partition的replica都确保放在了不同的zone中。本文逐步深入探讨了Swift如何通过

  7. swift皮筋弹动发射飞机ios源码

    这是一个款采用swift实现的皮筋弹动发射飞机游戏源码,游戏源码比较详细,大家可以研究学习一下吧。

  8. swift 写的app 源码,保存一下下

    http://www.topthink.com/topic/3345.htmlhttp://www.csdn.net/article/2015-01-09/2823502-swift-open-source-libs

  9. swift 源码网站 code4app

    http://code4app.com/ios/HTHorizontalSelectionList/54cb2c94933bf0883a8b4583http://123.th7.cn/code/DMPagerViewController_2522.html

  10. OpenStack Swift源码导读:业务整体架构和Proxy进程

    OpenStack的源码分析在网上已经非常多了,针对各个部分的解读亦是非常详尽。其中proxy是前端的业务接入进程。account、container和object目录分别是账户、容器和对象的业务处理逻辑进程。各个业务进程或模块之间的逻辑关系可以参考《OpenstackSwift简介》文中的架构图。在《OpenstackSwift简介》从理论上面介绍了具体的节点寻找过程。

随机推荐

  1. 【cocos2d-x 3.x 学习笔记】对象内存管理

    Cocos2d-x的内存管理cocos2d-x中使用的是上面的引用计数来管理内存,但是又增加了一些自己的特色。cocos2d-x中通过Ref类来实现引用计数,所有需要实现内存自动回收的类都应该继承自Ref类。下面是Ref类的定义:在cocos2d-x中创建对象通常有两种方式:这两中方式的差异可以参见我另一篇博文“对象创建方式讨论”。在cocos2d-x中提倡使用第二种方式,为了避免误用第一种方式,一般将构造函数设为protected或private。参考资料:[1]cocos2d-x高级开发教程2.3节[

  2. 利用cocos2dx 3.2开发消灭星星六如何在cocos2dx中显示中文

    由于编码的不同,在cocos2dx中的Label控件中如果放入中文字,往往会出现乱码。为了方便使用,我把这个从文档中获取中文字的方法放在一个头文件里面Chinese.h这里的tex_vec是cocos2dx提供的一个保存文档内容的一个容器。这里给出ChineseWords,xml的格式再看看ChineseWord的实现Chinese.cpp就这样,以后在需要用到中文字的地方,就先include这个头文件然后调用ChineseWord函数,获取一串中文字符串。

  3. 利用cocos2dx 3.2开发消灭星星七关于星星的算法

    在前面,我们已经在GameLayer中利用随机数初始化了一个StarMatrix,如果还不知道怎么创建星星矩阵请回去看看而且我们也讲了整个游戏的触摸事件的派发了。

  4. cocos2dx3.x 新手打包APK注意事项!

    这个在编译的时候就可以发现了比较好弄这只是我遇到的,其他的以后遇到再补充吧。。。以前被这两个问题坑了好久

  5. 利用cocos2dx 3.2开发消灭星星八游戏的结束判断与数据控制

    如果你看完之前的,那么你基本已经拥有一个消灭星星游戏的雏形。开始把剩下的两两互不相连的星星消去。那么如何判断是GameOver还是进入下一关呢。。其实游戏数据贯穿整个游戏,包括星星消除的时候要加到获得分数上,消去剩下两两不相连的星星的时候的加分政策等,因此如果前面没有做这一块的,最好回去搞一搞。

  6. 利用cocos2dx 3.2开发消灭星星九为游戏添加一些特效

    needClear是一个flag,当游戏判断不能再继续后,这个flag变为true,开始消除剩下的星星clearSumTime是一个累加器ONE_CLEAR_TIME就是每颗星星消除的时间2.连击加分信息一般消除一次星星都会有连击信息和加多少分的信息。其实这些combo标签就是一张图片,也是通过控制其属性或者runAction来实现。源码ComboEffect.hComboEffect.cpp4.消除星星粒子效果消除星星时,为了实现星星爆裂散落的效果,使用了cocos2d提供的粒子特效引擎对于粒子特效不了

  7. 02 Cocos2D-x引擎win7环境搭建及创建项目

    官网有搭建的文章,直接转载记录。环境搭建:本文介绍如何搭建Cocos2d-x3.2版本的开发环境。项目创建:一、通过命令创建项目前面搭建好环境后,怎样创建自己的Cocos2d-x项目呢?先来看看Cocos2d-x3.2的目录吧这就是Cocos2d-x3.2的目录。输入cocosnew项目名–p包名–lcpp–d路径回车就创建成功了例如:成功后,找到这个项目打开proj.win32目录下的Hello.slnF5成功了。

  8. 利用cocos2dx 3.2开发消灭星星十为游戏添加音效项目源码分享

    一个游戏,声音也是非常的重要,其实cocos2dx里面的简单音效引擎的使用是非常简单的。我这里只不过是用一个类对所有的音效进行管理罢了。Audio.hAudio.cpp好了,本系列教程到此结束,第一次写教程如有不对请见谅或指教,谢谢大家。最后附上整个项目的源代码点击打开链接

  9. 03 Helloworld

    程序都有一个入口点,在C++就是main函数了,打开main.cpp,代码如下:123456789101112131415161718#include"main.h"#include"AppDelegate.h"#include"cocos2d.h"USING_NS_CC;intAPIENTRY_tWinMain{UNREFERENCED_ParaMETER;UNREFERENCED_ParaMETER;//createtheapplicationinstanceAppDelegateapp;return

  10. MenuItemImage*图标菜单创建注意事项

    学习cocos2dx,看的是cocos2d-x3.x手游开发实例详解,这本书错误一大把,本着探索求知勇于发现错误改正错误的精神,我跟着书上的例子一起调试,当学习到场景切换这个小节的时候,出了个错误,卡了我好几个小时。

返回
顶部