(转载请注明原文:http://blog.csdn.net/while0/article/details/25615685)

本文章特指使用C++作为编程语言。基于cocos2dx游戏引擎开发游戏。

在cocos2dx中,sprite作为精灵类是使用最为频繁的类,与其他类相比。如:Node, Layer或Scene。Sprite最大的不同是它包括一个纹理。通过OpenGL的渲染。在游戏中呈现出来。

游戏中的主角。怪物,背景,或是精灵的血条等都是通过Sprite来实现的。

在cocos2dx中,关于创建Sprite的类,依据输入參数的不同有下面几个工厂函数。

    static Sprite* create()
static Sprite* create(const std::string& filename)
static Sprite* create(const std::string& filename, const Rect& rect)
static Sprite* createWithTexture(Texture2D *texture)
static Sprite* createWithTexture(Texture2D *texture, const Rect& rect, bool rotated=false)
static Sprite* createWithSpriteFrame(SpriteFrame *spriteFrame)
static Sprite* createWithSpriteFrameName(const std::string& spriteFrameName)

顺便提醒,工厂函数中Sprite的实例都调用了autorelease(),所以由工厂函数返回的实例一般马上通过addChild到增加到父节点中,这样它的引用计数加一,表示有人在使用这个实例,这个实例不能被释放。

当然。假设你不想马上增加到父节点中,也能够调用retain()函数直接对其引用计数加1,不然在主循环的下一次,就会将其回收,这是cocos2dx内存自己主动回收的机制决定的。

在游戏的开发过程中,使用Sprite最频繁的编码就是三部曲 (1)创建Sprite (2)增加layer中 (3)设置精灵位置:

Sprite *sprite = Sprite::create("hero.png");
this->addChild(sprite);
sprite->setPosition(Point(100, 200));

第一行创建Sprite的实例,也就是创建了一个类Sprite的对象,这个是在工厂函数中实现的。

    Sprite *sprite = new Sprite();
if (sprite && sprite->initWithFile(filename))
{
sprite->autorelease();
return sprite;
}
CC_SAFE_DELETE(sprite);
return nullptr;

我们重点关注第一句:

Sprite *sprite = new Sprite();

它决定了工厂函数创建的实例是Sprite类型,所以当在编码中调用Sprite的虚函数时,调用的就是Sprite类中的对应函数。

可能你会有这样一个问题:假设我想重载一个虚函数,在这个函数中实现自己须要的一些特殊行为,该怎么做?

答案是:必须继承Sprite创建一个你自己精灵类,在类中重载这个虚函数,这样还不够,必须定义这个新的继承类的工厂函数,在这个工厂函数中创建这个类的对象,

class MySprite : public Sprite
{
static MySprite * create(const char *pszFileName);
} MySprite * MySprite ::create(const char *filename)
{
MySprite *sprite = new MySprite();
if (sprite && sprite->initWithFile(filename))
{
sprite->autorelease(); return sprite;
}
CC_SAFE_DELETE(sprite);
return nullptr;
}

在程序中使用MySprite的代码与Sprite类似。

MySprite *sprite = MySprite::create("hero.png");
this->addChild(sprite);
sprite->setPosition(Point(100, 200));

假设输入參数不同。能够參考Sprite类的实现。创建不同输入參数的工厂函数。

所以,依照眼下cocos2dx的设计,在Sprite的继承类中,必须又一次实现新类的工厂函数,在工厂函数中创建新类的实例。这样新类中重载的虚函数就会被调用到。





使用C++编程,常常须要继承Sprite来定制和封装游戏特定的精灵。

有时候可能须要在Sprite的基础上进行多次继承,来达到类重用的目的。

本文以下提供两种方法,提供给读者參考。

在这两种方法中,不须要在继承类中又一次实现工厂函数。

  1. 在工厂函数中,实例作为输入參数传入
MySprite * MySprite ::create(Sprite* sprite, const char *filename)
{
if (sprite && sprite->initWithFile(filename))
{
sprite->autorelease();
return sprite;
}
CC_SAFE_DELETE(sprite);
return nullptr;
}

调用MySprite的代码:

MySprite *sprite = new MySprite();
MySprite::create(sprite, "hero.png");
this->addChild(sprite);
sprite->setPosition(Point(100, 200));

在MySprite的基础上再做继承时,就不须要实现工厂函数了。



比如:从MySprite继承新类MySprite2:

class MySprite2 : public MySprite
{
...
}

调用MySprite2的代码:

MySprite2 *sprite = new MySprite2();
MySprite::create(sprite, "hero.png");
this->addChild(sprite);
sprite->setPosition(Point(100, 200));

2. 创建newInstance的虚函数

另外一种方法,我们能够通过虚函数newInstance()来避免反复创建工厂函数。

在newInstance中创建新类的对象。

class MySprite : public Sprite
{
static MySprite * create(const char *filename);
virtual Sprite* newInstance();
}
Sprite* MySprite::newInstance()
{
return new MySprite();
} MySprite * MySprite ::create(const char *filename)
{
MySprite *sprite = newInstance();
if (sprite && sprite->initWithFile(filename))
{
sprite->autorelease(); return sprite;
}
CC_SAFE_DELETE(sprite);
return nullptr;
}

调用MySprite的代码:

MySprite::create("hero.png");
this->addChild(sprite);
sprite->setPosition(Point(100, 200));

另外一种方法的优点是在使用MySprite时与Sprite的全然同样,但须要在继承类中实现虚函数newInstance()。



本文抛砖引玉,在您的实现过程中或许会有更好的方法,欢迎讨论,共同进步。

关于在cocos2dx中继承Sprite的分析与技巧的更多相关文章

  1. Cocos2d-x中由sprite来驱动Box2D的body运动(用来制作平台游戏中多变的机关)

    好久都没写文章了,就来一篇吧.这种方法是在制作<胖鸟大冒险>时用到的.<胖鸟大冒险>中使用Box2D来进行物理模拟和碰撞检測,因此对每一个机关须要创建一个b2body.然后&l ...

  2. cocos2dx lua中继承与覆盖C++方法

    cocos2dx的extern.lua中的class方法为lua扩展了面向对象的功能,这使我们在开发中可以方便的继承原生类 但是用function返回对象的方法来继承C++类是没有super字段的,这 ...

  3. 2018.3.3 多线程中继承Thread 和实现Runnable接口 的比较(通过售票案例来分析)

    多线程中继承Thread 和实现Runnable接口 的比较(通过售票案例来分析) 通过Thread来实现 Test.java package com.lanqiao.demo4; public cl ...

  4. cocos2d-x中绘制3D图形--3D ToolKit for cocos2dx实现原理

    首先:了解具体情况请看这里:https://github.com/wantnon2/3DToolKit-for-cocos2dx 在看代码之前,最好还是先把项目git下来执行一下demoproject ...

  5. cocos2d-x 中的基本概念

    在 cocos2d-x 开头配置(Windows 平台)中,介绍了新建工程,这篇就介绍下 cocos2d-x 的一些概念.(前提是需要有C++的面向对象的基本知识和C++11的常用知识) 层,场景,导 ...

  6. cocos2d-x中的Box2D物理引擎

    在Cocos2d-x中集成了2个物理引擎,一个是Chipmunk,一个是Box2D.前者是用C语言编写的,文档和例子相对较少:Box2D是用C++写的,并且有比较完善的文档和资料.所以在需要使用物理引 ...

  7. cocos2dx Scene,Layer,Sprite的理解

    layer,scene,sprite的默认锚点都是0.5,0.5 三者都继承自Node节点,暂时没看出有什么区别,或者下面的话是对的吧. 在cocos2d-x中,一个应用可以有多个scene,但任何时 ...

  8. 3 cocos2dx 3.0 源码分析-mainLoop详细

    简述:   我靠上面图是不是太大了, 有点看不清了.  总结一下过程: 之前说过的appController 之后经过了若干初始化, 最后调用了displayLinker 的定时调用, 这里调用了函数 ...

  9. cocos2dx中的内存管理方式

    转载:http://www.cocoachina.com/bbs/read.php?tid=195219 今天看了一下cocos2dx的内存管理机制,有些地方不太好理解搞了挺长的时间,现在感觉自己理解 ...

随机推荐

  1. Session累计用户数据列表

    OrderForm.html <body>  <center>  <h1 ><font size="20">Order Items& ...

  2. BZOJAC400题留念

    BZOJAC400题了...

  3. Vmware虚拟机时间不准问题

    测试程序时碰到虚拟机经常时间不准,深受困扰,后来发现虚拟机有一个设置可以同步虚拟机和宿主机的时间: 该功能需要vmware tools安装成功才能有效.vmware tools的安装就不多细说了,至于 ...

  4. 使用Lock实现信号量

    public class SemaphoreOnLock {     private final Lock lock = new ReentrantLock();         private fi ...

  5. C++使用之常量的定义

    在介绍C++的常前,先看下下面的代码. for (int i = 0; i < 512; ++i) { …; } 512是什么,它具有什么含义?在代码中若直接使用类似512这些“魔数”(magi ...

  6. python 面试相关

    python单例模式: Python真的需要单例模式吗?我指像其他编程语言中的单例模式. 答案是:不需要!  因为,Python有模块(module),最pythonic的单例典范.模块在在一个应用程 ...

  7. Sharepoint 2013 启用搜做服务

    参考文件: http://www.cnblogs.com/jianyus/archive/2013/02/04/2891801.html 1. 创建好网站集,进入网站内容,点击搜素,会出现如下错误:( ...

  8. Oracle如何实现跨数据库查询

    转发:http://www.linuxidc.com/Linux/2012-02/53974.htm 实现结果:在一个数据库中某个用户下编写一个存储过程,在存储过程中使用DBLINK连接另一个数据库, ...

  9. lightOJ 1047 Neighbor House (DP)

    lightOJ 1047   Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...

  10. myeclipse 8.6 插件安装之SVN

    在这里我要说明一点,myEclipse 8.6的插件安装和之前的版本可能会有一些区别,下面是SVN插件的安装: 1.从官网下载site-1.6.13.zip文件,网址是:subclipse.tigri ...