Cocos2d-x How to save the user's game data XML file whether there is a problem judgment method _c language

Source: Internet
Author: User
Tags flush

Have written a blog before has introduced the game data save Class Ccuserdefault, mainly introduced several functions the use, is very simple, does not understand first reads this article. But when you actually do the project, it turns out that how to judge the existence of XML files is a problem, imagine the end of the game users want to open the app to continue to play, but found that still have to start again, is not going to be very disappointed ah, so we have to save the user's data, such as the highest score, sound settings or something. In this process how to determine the XML file is a problem, into my debugging found that the use of the engine provided by the function does not solve the problem, now use the following method, we look at the code.

The header file for the data-saving class
#ifndef _user_data_h_
#define _user_data_h_
#include "cocos2d.h"

using namespace cocos2d;

This class also uses the single example design mode

class UserData
{public
:
	//The following set and get methods are all used to set and obtain the game data of the
	bool Getmusicvalue ();
	void Setmusicvalue (bool b);
	Static UserData * Shareduserdata ();
	Called in the Appdelegate destructor to release the M_userdata data member
	void Freeuserdata ();
Private:
	///Save play background music member variable
	bool M_musicvalue;
	Static UserData * M_USERDATA;
	Perform some initialization operations in the constructor
	UserData ();

#endif
#include "UserData.h"//Below is the idiom for a single example design pattern, all of which are similar to each other, UserData * userdata::m_userdata = NULL;
	UserData * Userdata::shareduserdata () {if (M_userdata = NULL) {m_userdata = new UserData ();
return m_userdata;
		} void Userdata::freeuserdata () {if (m_userdata) {delete m_userdata;
	M_userdata = NULL; Initialize Userdata::userdata () {//) for game data in the UserData Constructor () () do not invoke Ccuserdefault::shareduserdefault ()->isxmlfileexist () To determine if there is an XML file, because when we call Shareduserdefalut () the//system will initialize the XML file for us, so the file will always exist if so called. And the use of ccuserdefault::isxmlfileexit () still solve the problem, this time whenever the startup program Isxmlfileexist () are returned is false, that is, the file will never exist, After my debugging, I don't think either of these functions will solve the problem//if you have any other findings please leave a message. Here I use the following methods to process, directly call the Getboolforkey function, the second argument is that if the isexit does not exist, then return false//so that the first time the user to play the game returned false, in the IF, some data initialization, and remain in the XML file, the second call will execute else//that is, the user's game data from the XML file is taken if (! M_musicvalue = Ccuserdefault::shareduserdefault ()->getboolforkey ("Isexit", False)) {Ccuserdefault::
		Shareduserdefault ()->setboolforkey ("Isexit", true); Initializes the game data and savesM_musicvalue = True in the XML file;
	Ccuserdefault::shareduserdefault ()->setboolforkey ("M_musicvalue", M_musicvalue);
	else {m_musicvalue = Ccuserdefault::shareduserdefault ()->getboolforkey ("M_musicvalue");

}//Returns the user's sound data bool Userdata::getmusicvalue () {return this->m_musicvalue;}
	Set the user's sound data and save it in a file, and finally use flush to refresh the near-XML file with void Userdata::setmusicvalue (bool b) {this->m_musicvalue = b;
	Ccuserdefault::shareduserdefault ()->setboolforkey ("M_musicvalue", This->m_musicvalue);
The implementation of this function on the Win32 is an empty Ccuserdefault::shareduserdefault ()->flush (); }

The code above is my small example of a ninja darts in a class, now just added to save the sound data code, but also improve some other questions, then write another blog, you will see the complete code.

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.