当前位置:网站首页>Cocos2d-x game archive [easy to understand]

Cocos2d-x game archive [easy to understand]

2022-07-07 20:45:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm the king of the whole stack .

The game archive function can save the data in the game . Let players continue the game . Single game is more important . and CCUserDefault It can be used as a lightweight database , Used to store data , Support data types bool,int, float, double, string.

Store the data

	CCUserDefault::sharedUserDefault()->setStringForKey("string", "value1"); // Parameter order : key . The corresponding value 
	CCUserDefault::sharedUserDefault()->setIntegerForKey("integer", 10);
	CCUserDefault::sharedUserDefault()->setFloatForKey("float", 2.3f);
	CCUserDefault::sharedUserDefault()->setDoubleForKey("double", 2.4);
	CCUserDefault::sharedUserDefault()->setBoolForKey("bool", true);

Get the above data output

	string str = CCUserDefault::sharedUserDefault()->getStringForKey("string");// Parameters : Return key value according to the passed in key 
	CCLOG("str: %s", str.c_str());
	int i = CCUserDefault::sharedUserDefault()->getIntegerForKey("integer");
	CCLOG("i: %d", i);
	float f = CCUserDefault::sharedUserDefault()->getFloatForKey("float");
	CCLOG("f: %f", f);
	double d = CCUserDefault::sharedUserDefault()->getDoubleForKey("double");
	CCLOG("d: %f", d);
	bool b = CCUserDefault::sharedUserDefault()->getBoolForKey("bool");
	if (b)
		CCLOG("bool is true");
	else
		CCLOG("bool is false");

Change data

	CCUserDefault::sharedUserDefault()->setStringForKey("string", "test"); // changes string data 
	CCUserDefault::sharedUserDefault()->setFloatForKey("float", 3.1413);  // changes float data 

	string str1 = CCUserDefault::sharedUserDefault()->getStringForKey("string");
	CCLOG("str1: %s", str1.c_str());
	float f2 =CCUserDefault::sharedUserDefault()->getFloatForKey("float");
	CCLOG("f2: %f", f2);

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116383.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207072002204888.html