Cocos2d-x之Array
| 版权声明:本文为博主原创文章,未经博主允许不得转载。
Array是一个列表类容器,是一种线性序列结构;列表容器中的元素是有序的,可以通过下标来访问,就和数组一样。其中Vector也是一种列表容器,但是Array的缺陷是不能自动增长,而Vector却可以。
Array的创建函数:
1 /** Creates an empty array. Default capacity is 10 创建_Array*/ 2 static __Array* create(); 3 /** Create an array with objects 使用一系列Ref创建_Array*/ 4 static __Array* create(Ref* object, ...) CC_REQUIRES_NULL_TERMINATION; 5 /** Create an array with one object 使用一个Ref创建_Array*/ 6 static __Array* createWithObject(Ref* object); 7 /** Create an array with a default capacity创建_Array,并设置容量 */ 8 static __Array* createWithCapacity(ssize_t capacity); 9 /** Create an array with from an existing array 用一个已经存在的_Array来创建另一个_Array*/ 10 static __Array* createWithArray(__Array* otherArray); 11 /** @brief Generate a Array pointer by file,param pFileName The file name of *.plist file 12 return The Array pointer generated from the file从属性列表文件创建_Array*/ 13 static __Array* createWithContentsOfFile(const std::string& pFileName); 14 /*@brief The same meaning as arrayWithContentsOfFile(), but it doesn't call autorelease, so the 15 invoker should call release().*/ 16 static __Array* createWithContentsOfFileThreadSafe(const std::string& pFileName);
常用添加元素函数:
向_Array对象中添加元素都必须是Ref和其子类的对象指针类型。
1 /** Add a certain object 添加一个元素*/ 2 void addObject(Ref* object); 3 /** Add all elements of an existing array 把一个已经存在的__Array对象中的所有元素添加到当前的__Array中*/ 4 void addObjectsFromArray(__Array* otherArray); 5 /** Insert a certain object at a certain index在指定的位置插入元素,ssize_t是int类型的别名*/ 6 void insertObject(Ref* object, ssize_t index);
常用移除函数:
1 /** Remove last object 移除最后一个元素*/ 2 void removeLastObject(bool releaseObj = true); 3 /** Remove a certain object 移除Array中的某个元素*/ 4 void removeObject(Ref* object, bool releaseObj = true); 5 /** Remove an element with a certain index 移除一个指定位置的元素*/ 6 void removeObjectAtIndex(ssize_t index, bool releaseObj = true); 7 /** Remove an objects 移除某个数组_Array对象*/ 8 void removeObjectsInArray(__Array* otherArray); 9 /** Remove all elements 移除所有的元素*/ 10 void removeAllObjects(); 11 /** Fast way to remove a certain object 快速移除某个元素,把数组的最后一个元素(数值的最后一个远足是NULL)赋值给要删除的元素,但是要注意,这会改变原有与元素的顺序*/ 12 void fastRemoveObject(Ref* object); 13 /** Fast way to remove an element with a certain index 快速的移除某个指定位置的元素,与fastRemoveObject函数类似*/ 14 void fastRemoveObjectAtIndex(ssize_t index);
替换和交换元素的函数
1 /** Swap two elements 交换两个元素*/ 2 void exchangeObject(Ref* object1, Ref* object2); 3 /** Swap two elements with certain indexes 交换两个指定位置的元素*/ 4 void exchangeObjectAtIndex(ssize_t index1, ssize_t index2); 5 /** Replace object at index with another object. 用一个对象替代指定位置的元素*/ 6 void replaceObjectAtIndex(ssize_t index, Ref* object, bool releaseObject = true);
其他函数:
1 /** Revers the array 反转Array */ 2 void reverseObjects(); 3 /**判断Array对象是否相等*/ 4 bool isEqualToArray(__Array* otherArray); 5 /** Returns a random element 随机返回元素*/ 6 Ref* getRandomObject(); 7 /** Returns a Boolean value that indicates whether object is present in array.返回某个元素是否存在Array容器中*/ 8 bool containsObject(Ref* object) const; 9 /**统计Array的大小*/ 10 ssize_t count();
实例:
.h files
#ifndef _ARRAYTEST_SCENE_H_
#define _ARRAYTEST_SCENE_H_
#include "cocos2d.h"
class arrayTest : public cocos2d::Layer
{
private:
public:
static cocos2d::Scene* createScene();
virtual bool init();
void arrTest();
CREATE_FUNC(arrayTest);
};
#endif // _ARRAYTEST_SCENE_H_
.cpp files
#include "ArrayTest.h"
USING_NS_CC;
Scene* arrayTest::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = arrayTest::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
bool arrayTest::init()
{
if (!Layer::init())
{
return false;
}
arrTest();
return true;
}
void arrayTest::arrTest()
{
//1.创建一个Array
Array* arr = Array::create();
//2.添加元素
arr->addObject(String::create("I "));
arr->addObject(String::create("love"));
arr->addObject(String::create(" China!"));
arr->addObject(String::create("values = 123456"));
//3.获得大小
int size = arr->count();
CCLOG("The array size is : count = %d", size);
for (int i = 0; i < size; i++)
{
String* e = (String*)arr->getObjectAtIndex(i);
CCLOG("%s", e->getCString());
}
//4.根据索引获得某个元素
String* elem = (String*)arr->getObjectAtIndex(2);
CCLOG("%s", elem->getCString());
//5.删除某个元素
arr->removeObjectAtIndex(3);
//6.遍历
size = arr->count();
for (int i = 0; i < size; i++)
{
String* e = (String*)arr->getObjectAtIndex(i);
CCLOG("%s", e->getCString());
}
}

浙公网安备 33010602011771号