1. Introduction
Recently due to various reasons, I haven't studied cocos2d-x for a long time. Suddenly have a sense of fear, so on, the previous learning about cocos2d-x a bit of fur will be completely forgotten, next time to learn, it is estimated that have to begin again. Therefore, we need to use it more often at ordinary times. Perseverance is the most important...
Since the direct use of Chinese characters in the cocos2d-x will appear garbled, who let the cocos2d-x is from cocos2d, and cocos2d this thing is written by foreigners? There are two methods to solve Chinese Garbled text:
(1) Using iconv library, cocos2d-x provides this library, but only on the win32 platform, when the program is transplanted to Android and other platforms, You need to download the iconv library compilation.
(2) write the Chinese string to the XML file (note that the XML file format is saved as a UTF-8), and then parse the XML file, the format can be modeled like the strings in Android. xml, and this method can facilitate internationalization in the future.
Through the comparison, we can easily find the second method is better, then the next introduces the second method, let us be able to use Chinese in the cocos2d-x program, completely solve the Chinese garbled problem.
Second Implementation
1 New XML file, according to a certain format, enter the corresponding Chinese to display, and finally must use UTF-8 format to save the file
[Html]
<Dict>
<Key> Hello </key>
<String> hello! </String>
<Key> Info </key>
<String> I am hahaya. </String>
</Dict>
<Dict>
<Key> Hello </key>
<String> hello! </String>
<Key> Info </key>
<String> I am hahaya. </String>
</Dict>
2. Code Implementation
[Cpp]
/** Use Chinese in the cocos2d-x **/
// Use CCDictionary to read xml
CCDictionary * strings = CCDictionary: createWithContentsOfFile ("strings. xml ");
// Read the value objectForKey in the Hello key. Get the corresponding string according to the key.
Const char * hello = (CCString *) strings-> objectForKey ("Hello")-> m_sString.c_str ();
// Read the value in the Info key
Const char * info = (CCString *) strings-> objectForKey ("Info")-> m_sString.c_str ();
// Display
CCLabelTTF * labelHello = CCLabelTTF: create (hello, "Arial", 24 );
LabelHello-> setPosition (ccp (size. width/2, size. height-50 ));
This-> addChild (labelHello, 1 );
CCLabelTTF * labelInfo = CCLabelTTF: create (info, "Arial", 30 );
LabelInfo-> setPosition (ccp (size. width/2, size. height-100 ));
This-> addChild (labelInfo, 1 );
/** Use Chinese in the cocos2d-x **/
// Use CCDictionary to read xml
CCDictionary * strings = CCDictionary: createWithContentsOfFile ("strings. xml ");
// Read the value objectForKey in the Hello key. Get the corresponding string according to the key.
Const char * hello = (CCString *) strings-> objectForKey ("Hello")-> m_sString.c_str ();
// Read the value in the Info key
Const char * info = (CCString *) strings-> objectForKey ("Info")-> m_sString.c_str ();
// Display
CCLabelTTF * labelHello = CCLabelTTF: create (hello, "Arial", 24 );
LabelHello-> setPosition (ccp (size. width/2, size. height-50 ));
This-> addChild (labelHello, 1 );
CCLabelTTF * labelInfo = CCLabelTTF: create (info, "Arial", 30 );
LabelInfo-> setPosition (ccp (size. width/2, size. height-100 ));
This-> addChild (labelInfo, 1 );
3. Program Running Effect