cocos2dx 3.x Value、Vector和Map的認識,cocos2dxvector

來源:互聯網
上載者:User

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出來也沒多久,書沒那麼快生產。。。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.