Cocos2d x is a method to determine if there is a problem with the XML file that holds the user's game data

  • 2020-04-02 02:46:10
  • OfStack

I have written a blog about the game data saving class CCUserDefault, mainly introduced the use of several functions, is very simple, do not know first look (link: #). However, it is a problem to judge the existence of XML files when we are actually working on the project. Just imagine that users would be disappointed if they want to open the APP to continue playing after the game, only to find that they have to start playing again. Therefore, we have to save the data of users, such as the highest score and the Settings of sound. In this process how to judge the XML file is a problem, through my debugging found that the function provided by the engine can not solve the problem, now use the following method, you see the code.


//The data is stored in the header file of the class
#ifndef _USER_DATA_H_
#define _USER_DATA_H_
#include "cocos2d.h"

using namespace cocos2d;

//This class also USES the singleton design pattern

class UserData
{
public:
	//The following set and get methods are used to set and get game data
	bool getMusicValue();
	void setMusicValue(bool b);
	static UserData * sharedUserData();
	//Called in the Appdelegate destructor to release the m_userData data member
	void freeUserData();
private:
	//Saves the member variable for whether to play background music
	bool m_musicValue;
	static UserData * m_userData;
	//Do some initialization in the constructor
	UserData();
};

#endif

#include "UserData.h"

//The following is a common way of writing the singleton design pattern. All singleton design patterns are similar
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 the game data in the constructor of UserData
UserData::UserData()
{

	//Don't call CCUserDefault: : sharedUserDefault () -> IsXMLFileExist () to determine if an XML file exists, because when we call sharedUserDefalut()
	//The system will initialize the XML file for us, so if we call it this way the file will exist forever. While using CCUserDefault: : isXMLFileExit () is still not solve
  //The problem is that whenever I launch the program isXMLFileExist(), it returns a false value, meaning that the file will never exist. After debugging, I think neither function can solve the problem
	//If you have any other findings, please leave a message. Here I do the following, calling the getboolean for key function directly, with the second argument saying that false is returned if isExit does not exist
	//This way, the user returns false the first time they play the game, some initialization of the data is done in the if, and it's kept in the XML file, and on the second call the else is executed
	//That is, the user's game data is taken from the XML file
	if(!(m_musicValue = CCUserDefault::sharedUserDefault()->getBoolForKey("isExit",false)))
	{
		CCUserDefault::sharedUserDefault()->setBoolForKey("isExit",true);
		//Initializes the game data and stores it in an XML file
		m_musicValue = true;
		CCUserDefault::sharedUserDefault()->setBoolForKey("m_musicValue",m_musicValue);
	}
	else
	{
		m_musicValue = CCUserDefault::sharedUserDefault()->getBoolForKey("m_musicValue");
	}
}

//Returns the user's voice data
bool UserData::getMusicValue()
{
	return this->m_musicValue;
}

//Set the user's sound data, save it in a file, and finally flush the near XML file with flush
void UserData::setMusicValue(bool b)
{
	this->m_musicValue = b;
	CCUserDefault::sharedUserDefault()->setBoolForKey("m_musicValue",this->m_musicValue);
	//The implementation of this function is empty on win32
	CCUserDefault::sharedUserDefault()->flush();
}

The code above is a class from my little example of ninja darts, and now it just adds the code for storing the sound data, and it's working on some other things, and I'll write another blog post, and you'll see the full code.


Related articles: