Cocos2dx 3.0 improvement Article (6) A solution to Chinese display

Source: Internet
Author: User

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







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.