Cocos2d-x 3.0 game instance learning notes "tower guard" Step 4 --- Editor (3) -- coordinate Storage & file loading operations

Source: Internet
Author: User

New entry C ++ cocos2d-x3.0 tower anti-instance game

/* Description:

** 1. This game instance is the last game on the cocos2d-x game development journey, which is rewritten and noted down here with 3.0

** 2. I have also asked me about wood. He said: Write it as you like. First, do not copy the code completely. Second, You can note that it is a learning note-Good Guy.

** 3. Here With cocos2d-x 3.0 version rewriting, many places are different, but from the rewriting process also good learning cocos2d-x

*/

* ** All the code corresponding to each step and the resources used are packaged at the end.

* ** To avoid code overhead, the code in each step is marked. At first glance, the code is implemented in the first step to avoid errors and cannot be changed back (if not, git should be used again ?)

* ** You can follow the design idea (well, the name is too tall. The reality is this step to do) first self-implementation-cocos2d-x is so, the same function has many different implementation methods; first self-toss is quite good.

* ** For the convenience of porting to the mobile phone, Android testing is compiled for each step. Because the code can be compiled in Win32 many times, compilation will fail, the code is tested.

Note content:

1. Slightly analyze

2. Check the code

3. Next Content preview

4. Download Code and resources

5. File Operations are mainly involved here. The editor Section is also complete. The design of the game process is coming soon.

--- No effect, but file operations are invisible.


Here, we first classify the Code:


I. Analysis

1. We can first edit the implementation of different levels. Each nextlvl operation clears this layer and reloads the map.

2. Then we hook up with the file. For towerpos and monsterpos, we have the corresponding container to save it, but we need to save it to the file to prepare for the subsequent game loading.

Ii. Code

First, the process is simple. Edit the levels, add the deleteallpos function, and modify the function for changing the levels:

Here, switching a level is actually clearing the editing layer, re-loading the map of other levels, and editing --- (well, there are only three maps, so there are three levels Limited, and maps are all the same .......)

void TowerPosEditorLayer::deleteAllPos(){this->removeAllChildrenWithCleanup(true);m_monsterPosList.clear();m_towerPosList.clear();}void TowerPosEditorLayer::nextLvl(){if(m_iCurLevel == 3){return ;}deleteAllPos();m_iCurLevel ++;loadConfigFile();}void TowerPosEditorLayer::preLvl(){if(m_iCurLevel == 1){return;}deleteAllPos();m_iCurLevel --;loadConfigFile();}
This can also be used for testing.

Bytes ------------------------------------------------------------------------------------

But how can I not save the edited map? Besides, if I save the edited map after the first level, and then after the second level, I think there is a little modification to the first level. What should I do?

Use a coordinate file to operate posloadutil.

Here, there are two more functions: loading existing functions and outputting existing functions to files.

We need to make a small summary that this is a singleton class, a rough understanding of Singleton classes:

For example, if you make a movie, there is a movie in Beijing today, and there will be a movie in Shanghai tomorrow. Here we will look at it as two scenes. Different Scenes will never change: director should be a singleton class (I understand this)

Let's create a singleton class by ourselves: exclusively supply the hotel; first, we don't have it. We just create one, which is static, that is, there is a whole film shooting process; wherever you go, make a call: getinstence () ---> deliver meal; OK

Mount:. h

Class posloadutil: Public node {public: static posloadutil * getinstance (); Virtual bool Init (); // ** 4 ** void puttofile (vector <posbase *> poslist, const char * sfilepath); // ** 4 ** select to pass the reference void loadposwithfile (vector <posbase *> & list, enumpostype postype, const char * sfilepath, node * container, int level, bool isdebug); Private: static posloadutil * _ posloadutil ;};
. CPP

# Include "posloadutil. H "optional * posloadutil: _ posloadutil = NULL; posloadutil * posloadutil: getinstance () {If (_ posloadutil = NULL) {_ posloadutil = new posloadutil (); if (_ posloadutil & _ posloadutil-> Init () {_ posloadutil-> autorelease ();} else {cc_safe_delete (_ posloadutil) ;}} return _ posloadutil ;} bool posloadutil: Init () {return true;} void posloadutil: puttofile (vector <posbase *> poslist, const char * SF Ilepath) {file * file = fopen (sfilepath, "W"); // fprintf (file, "<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> \ N "); fprintf (file," <! Doctype plist public \ "-// Apple // DTD plist 1.0 // en \" \ "http://www.apple.com/dtds/propertylist-1.0.dtd\"> \ n "); // fprintf (file, "<plist version = \" 1.0 \ "> \ n"); // <array> fprintf (file, "<array> \ n "); // each attribute posbase * posbase = NULL; For (Auto Ref: poslist) {posbase = dynamic_cast <posbase *> (REF); If (posbase! = NULL) {// <dict> fprintf (file, "<dict> \ n"); // <key> x </key> fprintf (file, "<key> x </key> \ n"); // <integer> 80 </Integer> fprintf (file, "<integer> %. 0f </Integer> \ n ", posbase-> getpos (). x); // <key> Y </key> fprintf (file, "<key> Y </key> \ n "); // <integer> 266 </Integer> fprintf (file, "<integer> %. 0f </Integer> \ n ", posbase-> getpos (). y); // </dict> fprintf (file, "</dict> \ n") ;}/// </array> fprintf (file, "</array> \ n"); // plist parent node field ends fprintf (file, "</plist> \ n"); fclose (File);} void posloadutil:: loadposwithfile (vector <posbase *> & list, enumpostype postype, const char * sfilepath, node * container, int ilevel, bool isdebug) {// read plist File Auto value_vector = fileutils:: getinstance ()-> getvaluevectorfromfile (sfilepath); For (Auto Ref: value_vector) {// convert the Object ref read in value_vector to the valuemap Type Auto temp_map = ref. asvaluemap (); // The converted valuemap object temp_map retrieves X, and the Y value is auto x = temp_map.at ("X "). asfloat (); Auto y = temp_map.at ("Y "). asfloat (); posbase * posbase = posbase: Create (CCP (x, y), postype, isdebug); list. pushback (posbase); container-> addchild (posbase, ilevel );}}
Then, modify it in the file output function in poseditorlayer:

Void poseditorlayer: outputpostoplistfile () {cclog ("outputpostoplistfile"); // output the turret coordinate configuration file _ string * stowerpospath = _ string :: createwithformat ("game/towerpos_level _ % d. plist ", _ curlevel); posloadutil: getinstance ()-> puttofile (m_towerposlist, stowerpospath-> getcstring ()); // output the monster coordinate configuration file _ string * smonsterpospath = _ string: createwithformat ("game/monsterpos_level _ % d. plist ", _ curlevel); posloadutil: getinstance ()-> puttofile (m_monsterposlist, smonsterpospath-> getcstring ());}
In the preload function of poseditorlayer:

Void poseditorlayer: preload () {/********************************** // load towerpos __ string * stowerpospath =__ string:: createwithformat ("game/towerpos_level _ % d. plist ", _ curlevel); posloadutil: getinstance ()-> loadposwithfile (m_towerposlist, entowerpos, stowerpospath-> getcstring (), this, 10, true ); /** // load monsterpos _ string * smonsterpospath = _ string: createwithformat ("game/monsterpos_level _ % d. plist ", _ curlevel); posloadutil: getinstance ()-> loadposwithfile (m_monsterposlist, enmonsterpos, smonsterpospath-> getcstring (), this, 10, true );}
Okay, you can test it happily. You can see the corresponding file in the corresponding directory.

Of course, even if there is no corresponding file in your initial directory, you will not load it.

Note: here, the route points of monsters must be edited in order before saving them. Otherwise, when the monsters are behind them, they will go in the order of points... Will go around

Iii. Next Content preview

After editing the map you want, you started the official game scene. After loading the coordinates of the gun station, you must place the gun in the position ..


----------------------------------------

4. Source Code & Resources

-----------------------------------------


Personal ignorance. Thank you for your correction and discussion.

Cocos2d-x 3.0 game instance learning notes "tower guard" Step 4 --- Editor (3) -- coordinate Storage & file loading operations

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.