cocos2d-x 中的数据结构集合(容器)(CCArray,Map,Vector)使用笔记

前端之家收集整理的这篇文章主要介绍了cocos2d-x 中的数据结构集合(容器)(CCArray,Map,Vector)使用笔记前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

第1部分、CCArray

cocos2d::CCArray(在3.0中已经过时,将被vector替代)是一个可变数组集合容器,放在其中的元素是有序的,并且可以重复,可以通过索引来获得元素,可以对容器内的元素进行添加删除

先看看教材中的示例:

//创建数组

CCArray * array=CCArray::create();

__String *s1=__String::create("Android");

__String *s2=__String::create("iOS");

__String *s3=__String::create("cocos2d-x");

//添加元素

array->addObject(s1);

array->addObject(s2);

array->addObject(s3);

//遍历元素

ssize_t count=array->count();

for (int i=0; i<count; i++) {

__String* item=(__String*)array->getObjectAtIndex(i);

log("元素%d %s",i,item->getCString());

}

//删除元素

array->removeObjectAtIndex(0);

log("删除0后--------");

ssize_t count1=array->count();

for (int i=0; i<count1; i++) {

__String* item=(__String*)array->getObjectAtIndex(i);

log("getCString());

}

运行效果如下:


元素0 Android

元素1 iOS

元素2 cocos2d-x

删除0后--------

元素0 iOS

元素1 cocos2d-x



下面,通过CCArray来存放26个字母,然后通过调用存放的单词来更新标签,示例如下:

首先通过CCArray申明一个数组,

CCArray* word_array=CCArray::createWithCapacity(26);

向容器中添加元素,这里添加一些字符串:

word_array->addObject(CCString::create("ant"));

word_array->addObject(CCString::create("bee"));

word_array->addObject(CCString::create("camel"));

word_array->addObject(CCString::create("dove"));

word_array->addObject(CCString::create("eagle"));

word_array->addObject(CCString::create("fox"));

word_array->addObject(CCString::create("gull"));

word_array->addObject(CCString::create("horse"));

word_array->addObject(CCString::create("i"));

word_array->addObject(CCString::create("j"));

word_array->addObject(CCString::create("k"));

word_array->addObject(CCString::create("l"));

word_array->addObject(CCString::create("m"));

word_array->addObject(CCString::create("n"));

word_array->addObject(CCString::create("o"));

word_array->addObject(CCString::create("p"));

word_array->addObject(CCString::create("q"));

word_array->addObject(CCString::create("r"));

word_array->addObject(CCString::create("s"));

word_array->addObject(CCString::create("t"));

word_array->addObject(CCString::create("u"));

word_array->addObject(CCString::create("v"));

word_array->addObject(CCString::create("w"));

word_array->addObject(CCString::create("x"));

word_array->addObject(CCString::create("y"));

word_array->addObject(CCString::create("z"));

注意当CCArray的容器建好后,如果不马上使用,需要通过retain()来进行保持,否则会被马上释放掉。

word_array->retain();


先申明一个标签Label* lable_word;

voidLearnABC::load_word_label(){

//读取userdefault初始化字母标签

int read_seleted_idx=UserDefault::getInstance()->getIntegerForKey("selected_idx",999);

Size size=Director::getInstance()->getWinSize();

if (read_seleted_idx==999) {

lable_word=Label::createWithTTF("please select letter!","fonts/arial.ttf",42);

}

else{

CCObject* itemstr=NULL;

itemstr=word_array->getObjectAtIndex(read_seleted_idx);

std::string label_Str=((CCString*)itemstr)->getCString();

lable_word=Label::createWithTTF(label_Str,"fonts/arial.ttf",42);

}

lable_word->setColor(Color3B::RED);

lable_word->setOpacity(90);

lable_word->setPosition(Vec2(size.width-230,size.height*0.25));

this->addChild(lable_word);

};


刷新label标签,根据数组的索引来更新标签

voidLearnABC::refresh_word_label(ssize_t idx){

CCObject* item=NULL;

log("idx is %zd",idx);

item=word_array->getObjectAtIndex(idx);

std::string label_Str=((CCString*)item)->getCString();

lable_word->Label::setString(label_Str); //将label进行更新

}


第2部分、Vector

cocos2d::Vector<T>是一个封装好的能动态顺序访问的容器,其中的元素是按序存取的。作为cocos2d::CCArray的替代品,请优先使用,

下面是实例:

Vector<Sprite*> vector;

Sprite *s1=Sprite::create();

s1->setTag(111);

vector.pushBack(s1);

Sprite *s2=Sprite::create();

s2->setTag(222);

vector.pushBack(s2);

Sprite *s3=Sprite::create();

s3->setTag(333);

vector.pushBack(s3);

for (auto s : vector) {

log("tag---->%d",s->getTag());

}


输出结果如下:

tag---->111

tag---->222

tag---->333



即然是替代CCArray,那我们来看看是如何完全取代第1部分中的代码的吧!

首先申明vector容器:

Vector<CCString*> word_vector;


然后向容器中添加对象元素:

word_vector.pushBack(CCString::create("ant"));

word_vector.pushBack(CCString::create("bee"));

word_vector.pushBack(CCString::create("camel"));

word_vector.pushBack(CCString::create("dove"));

word_vector.pushBack(CCString::create("eagle"));

word_vector.pushBack(CCString::create("fox"));

word_vector.pushBack(CCString::create("gull"));

word_vector.pushBack(CCString::create("horse"));

word_vector.pushBack(CCString::create("i"));

word_vector.pushBack(CCString::create("j"));

word_vector.pushBack(CCString::create("k"));

word_vector.pushBack(CCString::create("l"));

word_vector.pushBack(CCString::create("m"));

word_vector.pushBack(CCString::create("n"));

word_vector.pushBack(CCString::create("o"));

word_vector.pushBack(CCString::create("p"));

word_vector.pushBack(CCString::create("q"));

word_vector.pushBack(CCString::create("r"));

word_vector.pushBack(CCString::create("s"));

word_vector.pushBack(CCString::create("t"));

word_vector.pushBack(CCString::create("u"));

word_vector.pushBack(CCString::create("v"));

word_vector.pushBack(CCString::create("w"));

word_vector.pushBack(CCString::create("x"));

word_vector.pushBack(CCString::create("y"));

word_vector.pushBack(CCString::create("z"));


voidLearnABC::load_word_label(){

//读取userdefault初始化字母

int read_seleted_idx=UserDefault::getInstance()->getIntegerForKey("selected_idx",999);

42);

}

else{

// CCObject* itemstr=NULL;

// itemstr=word_array->getObjectAtIndex(read_seleted_idx);

// std::string label_Str=((CCString*)itemstr)->getCString();

CCObject* itemstr=NULL;

itemstr=word_vector.at(read_seleted_idx);

std::string label_Str=((CCString*)itemstr)->getCString();

lable_word=Label::createWithTTF(label_Str,42);

}

this->addChild(lable_word);

};


voidLearnABC::refresh_word_label(ssize_t idx){

CCObject* item=NULL;

//ssize_t wordindex=idx;

// item=word_array->getObjectAtIndex(idx);

// std::string label_Str=((CCString*)item)->getCString();

item=word_vector.at(idx);

std::string label_Str=((CCString*)item)->getCString();

lable_word->Label::setString(label_Str);

}


通过比较可以看出vector使用起来和ccarray一样方便。同时vector如果不马上使用时不会被释放掉,不需要手工进行保持。


vector.size()用来获取vector中元素的个数;

vector.at(index)用来获取索引为index的元素对象;


第3部分 map


cocos2d::Map<K,V>通过键值对方式来保存数据,元素是无序的。用来替代之前的CCDirtionary.示例如下:

Map<std::string,Sprite*> map;

std::string key1="testkey1";

Sprite* sprite1=Sprite::create();

sprite1->setTag(111);

map.insert(key1,sprite1);

std::string key2="testkey2";

Sprite* sprite2=Sprite::create();

sprite2->setTag(222);

map.insert(key2,sprite2);

std::string key3="testkey3";

Sprite* sprite3=Sprite::create();

sprite3->setTag(333);

map.insert(key3,sprite3);

std::string key4="testkey4";

Sprite* sprite4=Sprite::create();

sprite4->setTag(444);

map.insert(key4,sprite4);

Sprite* sprite=map.at(key1);

log("sprite1 tag=%d",sprite->getTag());

std::vector<std::string> keys=map.keys();

for (auto k:keys){

Sprite* s=map.at(k);

log("%s ---tag--- %d",k.c_str(),s->getTag());

}


输出结果如下:

sprite1 tag=111

testkey4 ---tag--- 444

testkey3 ---tag--- 333

testkey2 ---tag--- 222

testkey1 ---tag--- 111



{未完待续}

原文链接:/cocos2dx/341811.html

猜你在找的Cocos2d-x相关文章