cocos2d-x 3.0 中新的Vector类
来源:互联网 发布:网络商业通 编辑:程序博客网 时间:2024/05/22 11:50
(本文转自: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 (NSArray
, NSDictionary
, 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.hclass HelloWorld : public cocos2d::CCLayer{ // ... cocos2d::CCArray *_bullets;};// HelloWorld.cppusing 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 ofCCSprite*
s, for example. - they are allocated on the heap using
CCArray::create()
, and follow cocos2d-x memory management rules (must beretain()
ed andrelease()
ed).
the new, 3.0 way might look like this:
// HelloWorld.hclass HelloWorld : public cocos2d::CCLayer{ // other stuff... cocos2d::Vector<cocos2d::Sprite *> _bullets;};// HelloWorld.cppusing 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 ofcocos2d::Object
(in this example, we are usingcocos2d::Sprite *
— pointers toSprite
s)._bullets
, itself, is no longer a pointer. it’s a statically allocated object: nonew
orcreate
here. in general, the new container classes shouldn’t be allocated on the heap.- we’re initializing
_bullets
inHelloWorld
‘s constructor. if you wanted to initialize it in theinit()
method instead, you could use something like:this->_bullets = cocos2d::Vector<cocos2d::Sprite *>{100};
. again, note that there is nonew
orcreate
involved.
somewhere in some shooting code you might have something like this:
cocos2d-x v2.x:
// add a bulletCCSprite *bullet = CCSprite::create("circle.png");this->_bullets->addObject(bullet); // retains bullet
cocos2d-x v3.0 beta:
// add a bulletSprite *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 bulletsCCObject *obj;CCARRAY_FOREACH(this->_bullets, obj){ CCSprite *bullet = (CCSprite *)obj; // do something with bullet. if (bullet->getPositionX() > 160) { // ... }}
v3.0beta:
// loop through bulletsfor (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}
- cocos2d-x 3.0 中新的Vector类
- 新发布的 cocos2d-x 3.0 与 cocos2d-x 差异
- Cocos2d-x 3.0-新的Label-Cocos2d-x官方文档
- cocos2d-x Vector的使用
- Cocos2d-x 3.0 新的点染架构
- Cocos2d-x 3.0 新的渲染架构
- COCOS2D-X 3.0新的数据结构
- Cocos2d-x 3.0 新的渲染架构
- cocos2d-x 3.0开发笔记---vector的使用
- cocos2d-x 3.0开发笔记---vector的使用
- cocos2d-x 新的开始
- [Cocos2d-x v3.x]Mac OX 创建新的Cocos2d-x 3.0 项目
- Cocos2d-x 3.0final -关于V3中CallFunc::create的新的使用方法
- Cocos2d-x 3.0-关于V3中CallFunc::create的新的使用方法
- cocos2d-x 3.0新特性
- 总结Cocos2d-x 3.0 中新字体标签Label
- 总结Cocos2d-x 3.0 中新字体标签Label
- 总结Cocos2d-x 3.0 中新字体标签Label
- 01-计算机网络概述之发展简史01-lh
- c++的类成员权限以及友元
- Django & Google App Engine & Blogs
- HT实习三个月个人总结
- 文本分类入门(十一)特征选择方法之信息增益
- cocos2d-x 3.0 中新的Vector类
- 员工工资
- 利用 Cocos2dx-3.0 新物理特性模拟弹珠迷宫
- 0101-APP-Alarm -controller
- u-boot-2009.08在飞凌OK2440/FL2440上的移植(五)——让u-boot从nand flash启动
- 8051 P0口分时复用详解
- 谈一下如何设计Oracle 分区表
- Memcached 内存分配机制介绍
- Struts 2标签库