Cocos2dx tips (7) static data

Source: Internet
Author: User

At noon, I bought two books from Dangdang, one of which is "a hundred years of loneliness" for reading; the other is "Zhu Xian 2" for collecting.

I have two thick stacks of books on my desk. Most of them are non-technical books. Why? The technical books are expensive! (Joke ). Reading is a meaningful thing and a pleasure of my childhood. After all, life is so rich and beautiful. I read several pages of books every day before going to bed, and occasionally read technical books. Of course, this is a minority case. In my opinion, if programmers cannot pull themselves out of the world of code during their work, they may always be just a programmer.

The predecessors said: the programmer's image is dirty, flip-flops + beach pants, so you wear it like this. The predecessors said: it is difficult for programmers to find a girlfriend. Maybe your love view has changed, when I was free, I came up with Mr. Cang. The predecessors said that programmers are not programmers but programmers, So you agree with this statement and are complacent with this title.
In fact, you must always think that your career is noble and is separated from low-level interests. Occasionally, I should say goodbye to my right hand and try to wear a formal dress, suit and shoes, and feel confident. What is wearing, senma casual wear.


It seems to be far away. Return to the topic!
-----------------------
This article describes how to use static data. Yes. Do you think the words static data are very technical or static. Haha.
So what is static data? Static data is the read-only data in the program. For example, there are some NPCs in our game. Their names, pictures, and characters are fixed and will not change during the game, for example, it is impossible for you to say that in the game "Fu Jia Feng Shen Chuan", which one of the following is Li Jing's Lao Tzu who beat Li Jing? Of course, during game development, the attribute values of these NPCs may change frequently. For example, if the baseline blood volume of the NPCs is 100, then the 100 is too small, so you decided to change it to 101. To facilitate modification, the common practice is to store the data in an external file.
Here we choose to use plist to save static data and then use the following method to read plist data:

Auto dic = Dictionary: createWithContentsOfFile (std: string & pFileName); // read to the Dictionary and store it in the cache. // Or auto array = Array: createWithContentsOfFile (std: string & pFileName); // read from the array.
The following uses a small example to briefly describe how to use it:
For example The wolf who falls in love with pleasant goatTwo NPCs, pleasant goat and Big Wolf, respectively. The IDS in plist are 10 and 20 respectively.As follows:
 
 
  
   
    
Id
   
   
    
10
   
   
    
Name
   
   
    
Pleasant goat
   
   
    
Image
   
   
    
Xiyy.png
   
   
    
Info
   
   
    
Is a goat, constellation: Aries, like to eat by the Big Wolf
   
  
  
   
    
Id
   
   
    
20
   
   
    
Name
   
   
    
Grey Wolf
   
   
    
Image
   
   
    
Huitl.png
   
   
    
Info
   
   
    
Wolf from the north, constellation: Orion, like to eat pleasant goat
   
  
 
In plist, the NPC data exists in array, so here I choose to use Array to read the data.The process is as follows:
Auto m_array = Array: createWithContentsOfFile ("info. plist "); // read plist data, stored in the m_array array // traverse the m_array, and find the desired NPC information through the id of the NPC for (int I = 0; I
 
  
Count (); I ++) {// from the plist content above, we can see that the data of each NPC is saved in the array in dic (dictionary) mode. auto dic = static_cast
  
   
(M_array-> getObjectAtIndex (I); int id = (static_cast
   
    
(Dic-> objForKey ("id")-> intValue (); // find the key value corresponding to the id from the dictionary, convert to int type // If id = 10, this is the idif (id = 10) {std: string name_str = (static_cast
    
     
(Dic-> objForKey (name)-> getString (); // read the name std: string image_str = (static_cast
     
      
(Dic-> objForKey (name)-> getString (); // read the IMG image name std: string info_str = (static_cast
      
        (Dic-> objForKey (name)-> getString (); // read the introduction of pleasant goat... // follow the instructions below }}
      
     
    
   
  
 


Hey. It's over.

Very short, but can be rotated.


Respect Original, reprinted please indicate Source: http://blog.csdn.net/start530/article/details/23301233

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.