Cocos2d-x specific changes
Following are changes Cocos2d-X specific.
CCDictionary and CCArray
1. Why CCMutableDictionary is Deprecated?
In gles20 branch, 'CCMutableDictionary' and 'CCMutableArray' are deprecated. You should use CCDictionary and CCArray instead.
CCDictionary is implemented by UTHash. Compared with the old CCMutableDictionary which is implemented by stl, the efficiency of the CCDictionary
is increased by at least two times. Also, CCDictionary now does not use template of cpp, so it can easily be bound to script.
Currently, CCDictionary supports two kinds of key type, which are 'std::stirng' and 'int'. One CCDictionary instance only supports one unique key type.
It is confirmed once you first invoke 'setObject'.
The way of using CCDictionary is almost the same as CCMutableDictionary. We kept the same api and removed the 'begin', 'end' and 'next' method which are used in iterating the whole dictionary. Instead, we use CCDICT_FOREACH macro to achieve that. The way of using CCDICT_FOREACH is quite the same as CCARRAY_FOREACH. The following will illustrate how to iterate CCDictionary :
----------------------------------------------------------------------------------------------------------------------------------------------------------------------- CCMutableDictionary<std::string, CCObjectSubClass*> | CCDictionary ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- std::vector<std::string> keys = theDict->allKeys(); | CCDictElement* pElement = NULL; std::vector<std::string>::iterator it; | CCDICT_FOREACH(theDict, pElement) for (it = keys.begin(); it != keys.end(); it++) | { { | CCObjectSubClass* pSubClassObj = (CCObjectSubClass*)pElement->getObject(); std::string oneKey = *it; | // You can also get the current key, but make sure you know the key type. CCObjectSubClass* pSubClassObj = theDict->objectForKey(oneKey); | std::string oneStrKey = pElement->getStrKey(); // if the key type is string. // do some operation by using pSubClass pointer. | // int oneIntKey = pElement->getIntKey(); // if the key type is integer. ... | // do some operation by using pSubClass pointer. ... | ... } | } | -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
We kept CCDictionary::allKeys method, so you can also use the same way as CCMutableDictionary to iterate dictionary, but we strongly don't advise you to do that.
Becasue the performance of CCDICT_FOREACH is much higher than getting all key and then using CCARRAY_FOREACH to traverse dictionary.
If you want to iterate CCDictionary at lua codes, of course, you can't use the CCDICT_FOREACH macro, in this case, you should use the old way to make it works.
4. Why CCMutableArray is deprecated?
In cocos2d-x 1.0.1, we have CCArray and CCMutableArray, which make users very confused,
and CCMutableArray also uses template and stl, the disadvantage is the same as CCMutableDictionary.
The old CCArray does not support reversely iterate array, we have provided the macro -- CCARRAY_FOREACH_REVERSE to achieve that.