I think everyone should have encountered this situation. When writing code in VS, You need to display Chinese characters in the Label, the Code is as follows:
Auto m_label = LabelTTF: create ("Happy New Year! "," Arial ", 25); m_label-> setPosition (Point (100,100); this-> addChild (m_label );
When debugging, we found that m_label displays a bunch of incredible garbled characters, which means that the simulator does not support Chinese display methods (in fact, I found that many of my words and phrases are not strictly described, you can understand it ). I tried to solve this problem, but all of them ended in failure. Finally, when I asked some experts, I suddenly realized that writing such code should develop a good habit, that is, do not directly input Chinese in the code, instead, you should write the Chinese statements used in the game to a document. When you need to use Chinese, you should read them in the document. This method not only solves the problem of Chinese display, but also facilitates later statement modification.
Let me take the plist document as an example.
Assume that there is a document named Chinese. plist in xml format, as shown below:
Id
10
Info
Happy New Year!
Id
20
Info
Money now!
Next we will explain how to extract these Chinese characters.
1. Read the plist document to the array first.
auto array_plist = Array::createWithContentsOfFile("Chinese.plist");
2. Create a dictionary to extract "Happy New Year !" These words
For (int I = 0; icount (); I ++) {auto dic = (Dictionary *) array_plist-> getObjectAtInder (I ); // obtain the first object int key_id = 10 in array_plist; // because in the plist document, "Happy New Year!" The corresponding id is 10int key_id_plist = (String *) dic-> objForKey ("id")-> intValue (); // obtain the value of "id" in the dictionary, convert String type to integer if (key_id = key_id_plist) {std: string info_str = (String *) dic-> objForKey ("info ")) -> getString (); // if the two IDs are equal, the object to be obtained is found, and information with the key value "info" is extracted }}
3. Well, the method is the above. It should be quite simple. Below is a complete section of code, which will be "Happy New Year !", "Money right away" is displayed.
Auto array_plist = Array: createWithContentsOfFile ("Chinese. plist "); std: string info1; // store the string std: string info2 for Happy New Year; // store the string for (int I = 0; icount (); I ++) {auto dic = (Dictionary *) array_plist-> getObjectAtInder (I); // obtain the first object int key_id1 = 10 in array_plist; // because in the plist document, "Happy New Year!" The corresponding id is 10int key_id2 = 20; // The idint key_id_plist = (String *) dic-> objForKey ("id")-> intValue (); // obtain the value of "id" in the dictionary and convert the value of String type to an integer if (key_id1 = key_id_plist) {info_str1 = (String *) dic-> objForKey ("info")-> getString (); // get the "Happy New Year" text continue;} if (key_id2 = key_id_plist) {info_str2 = (String *) dic-> objForKey ("info")-> getString (); // get "Get money right away" text continue ;}} auto label_1 = LabelTTF: create (info_str1, "Arial", 25); label_1-> setPosition (Point (100,100); this-> addChild (label_1, 1 ); auto label_2 = LabelTTF: create (info_str2, "Arial", 25); label_2-> setPosition (Point (100,200); this-> addChild (label_2, 1 );
Well, the running effect should be like the following ..........................
Actually, my code is written in word, so I didn't debug it, right...
By ~~
Tomorrow, Saturday, one day off. You can launch another public platform.
Reprinted please note address: http://blog.csdn.net/start530/article/details/18740733