cocos2dx 3.x Value、Vector和Map的認識,cocos2dxvector
1. Value
cocos2d::Value 是一個包含了很多原生類型(int,float,double,bool,unsigned char,char* 和 std::string)外
加std::vector<Value>, std::unordered_map<std::string,Value> 和 std::unordered_map<int,Value> 的類。
你可以把所有上面的提及的原生類型放入 cocos2d::Value 對象中,然後將它們轉化為對應的原生類型,反之亦然。
Value val; // 調用預設建構函式if (val.isNull()) { log("val is null");}else{ std::string str =val.getDescription(); log("The description of val0:%s",str.c_str());}//----------------------------------------------------Value val1(65); // 用一個 int 初始化//Value val1(3.4f); // 用一個 float 初始化//Value val1(3.5); // 用一個 double 初始化log("The description of the integer value:%s",val1.getDescription().c_str());log("val1.asByte() = %c",val1.asByte());//----------------------------------------------------std::string strV = "string";Value val2(strV); // 用 string 初始化log("The description of the string value:%s",val2.getDescription().c_str());//----------------------------------------------------auto sp0 = Sprite::create();Vector<Object*>* vecV = new Vector<Object*>();vecV->pushBack(sp0);Value val3(vecV); // 用 Vector 初始化log("The description of the Vector value:%s",val3.getDescription().c_str());delete vecV;//----------------------------------------------------Map<std::string, Object*>* mapV = new Map<std::string, Object*>();mapV->insert(strV,sp0);Value val4(mapV); // 用 Map 初始化log("The description of the Map value:%s",val4.getDescription().c_str());delete mapV;//----------------------------------------------------Value val6(&val4); // 用 Map 初始化log("The description of the Value-type value:%s",val6.getDescription().c_str());//----------------------------------------------------val2 = val1; // 在兩個不同指類型間賦值log("operator-> The description of val2:%s",val2.getDescription().c_str());val2 = 4; // 直接賦值log("operator-> The description of val4:%s",val2.getDescription().c_str());輸出:cocos2d: val is nullcocos2d: The description of the integer value:65cocos2d: val1.asByte() = Acocos2d: The description of the string value:stringcocos2d: The description of the Vector value:truecocos2d: The description of the Map value:truecocos2d: The description of the Value-type value:truecocos2d: operator-> The description of val2:65cocos2d: operator-> The description of val4:4
Value的作用和用法:在建立Value時,往建構函式裡傳入一個值,Value就會自動根據這個值來決定自己的類型。在擷取Value的值時,就根據它的類型,調用as**函數擷取。
整數、浮點型和字串之間的轉換
整型轉為字串: std::string str = "NO"+Value(1).asString();
字串轉為整型:log("%d",Value("1234").asInt())
浮點型轉字串:log("%s",Value(123.5f).asString().c_str())
字串轉浮點型:log("%f",Value("14.45").asFloat())
2. Vector
Vector是一個封裝好的能動態增長順序訪問的容器。
主要使用的函數說明:
size():Vector大小
at(index):返回Vector下標為index的對象
pushBack(object):在Vector的最後添加一個object對象
eraseObject(object):從Vector中移除object對象
erase(index):從Vector中移除下標為index的對象
clear():清空Vector
如何遍曆Vector
for(auto obj : vector){
...
}
3.Map
Map是一個儲存鍵值對的關聯式容器,它可以通過它們的鍵快速檢索對應的值。
主要函數:
insert(key,value):向Map中插入一個對象。
at(key):返回Map中關鍵字為key的對象
如何遍曆Map?
mapKeyVec = map1.keys(); for(auto key : mapKeyVec) { auto spTag = map1.at(key)->getTag(); log("The Sprite tag = %d, MAP key = %s",spTag,key.c_str()); log("Element with key %s is located in bucket %zd",key.c_str(),map1.bucket(key)); }
這本書是基於Cocos2dx的哪個版本,2x還是3x
這個是一個開源社區開發的,不是哪個公司開發的。
到它首頁上,看最下面一行就知道了。
“Cocos2D-x 開源開發人員社區”
這本書的範例程式碼是基於Cocos2dx的哪個版本寫的,是2x還是3x
肯定是2.X
畢竟3.0出來也沒多久,書沒那麼快生產。。。