cocos2d-x 3.0 中新的Vector类

(本文转自:ht[去掉我]tp://dev.bunnyhero.or[去掉我]g/2014/01/cocos2d-x-30-beta-the-new-vector-class/)


cocos2d-x 3.0 beta was recently released. one of the big new additions is the introduction of new container classes.

up until now, the cocos2d-x’s container classes were modelled after objective-c’s container classes (NSArrayNSDictionary, etc). now they are modelled after the standard c++ library’s containers (std::vector<T>std::map<T>).

this post will show you a simple example of converting code from using the oldCCArray class to the new Vector class.

consider this cocos2d-x v2.x code:

// HelloWorld.h
class HelloWorld : public cocos2d::CCLayer
{
    //  ...
    cocos2d::CCArray *_bullets;
};


// HelloWorld.cpp
using namespace cocos2d;

bool HelloWorld::init()
{
    if (!this->CCLayer::init())
        return false;

    //  ...

    //  bullets
    this->_bullets = CCArray::createWithCapacity(100);
    this->_bullets->retain();
}

let’s review some things about the old CCArray class:

  • they can only contain CCObject* (i.e. pointers to CCObject). you have to cast its elements to a subclass if you’re storing a bunch of CCSprite*s, for example.
  • they are allocated on the heap using CCArray::create(), and follow cocos2d-x memory management rules (must be retain()ed andrelease()ed).

the new, 3.0 way might look like this:

// HelloWorld.h
class HelloWorld : public cocos2d::CCLayer
{
    //  other stuff...

    cocos2d::Vector<cocos2d::Sprite *> _bullets;
};

// HelloWorld.cpp
using namespace cocos2d;

HelloWorld::HelloWorld() : _bullets(100)
{
}

things to note here:

  • cocos2d::Vector<T> is a template class. the type it contains must be a pointer to a subclass of cocos2d::Object (in this example, we are usingcocos2d::Sprite * — pointers to Sprites).
  • _bullets, itself, is no longer a pointer. it’s a statically allocated object: nonew or create here. in general, the new container classes shouldn’t be allocated on the heap.
  • we’re initializing _bullets in HelloWorld‘s constructor. if you wanted to initialize it in the init() method instead, you could use something like:this->_bullets = cocos2d::Vector<cocos2d::Sprite *>{100};. again, note that there is no new or create involved.

somewhere in some shooting code you might have something like this:

cocos2d-x v2.x:

//   add a bullet
CCSprite *bullet = CCSprite::create("circle.png");
this->_bullets->addObject(bullet);  //  retains bullet

cocos2d-x v3.0 beta:

//   add a bullet
Sprite *bullet = Sprite::create("circle.png")
this->_bullets.pushBack(bullet);  //  retains bullet

not much different here except for the name change (pushBack is named similarly to the STL’s push_back method), and the fact that you use “.” instead of “->” (because _bullets is no longer a pointer).

removing an item:

v2.x:

this->_bullets->removeObject(bullet);

v3.0beta:

this->_bullets.eraseObject(bullet);

looping through the array:

v2.x:

//  loop through bullets
CCObject *obj;
CCARRAY_FOREACH(this->_bullets, obj)
{
    CCSprite *bullet = (CCSprite *)obj;
    //  do something with bullet.
    if (bullet->getPositionX() > 160)
    {
        //  ...
    }

}

v3.0beta:

//  loop through bullets
for (auto bullet: this->_bullets)
{
    //  do something with bullet.
    //  no need to cast in this case
    if (bullet->getPositionX() > 160)
    {
        //  ...
    }
}

and finally, in your destructor, you’d want to clean up the array so as not to leak memory:

v2.x:

HelloWorld::~HelloWorld()
{
    //  release stuff
    this->_bullets->release();
}

v3.0beta:

HelloWorld::~HelloWorld()
{
    //  nothing to do! _bullets manages its storage automatically when it falls out of scope
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值