void HelloWorld::testCCUserDefault() { CCSize size = CCDirector::sharedDirector()->getWinSize(); CCLabelTTF* ttf = CCLabelTTF::create(" ", "Helvetica", 30); ttf->setPosition(ccp(size.width/2, size.height/2)); addChild(ttf); if (CCUserDefault::sharedUserDefault()->getBoolForKey("bool",false)) { ttf->setString("No user data"); //Load bool isBool = CCUserDefault::sharedUserDefault()->getBoolForKey("bool", false); double isDouble = CCUserDefault::sharedUserDefault()->getDoubleForKey("double", 0.0); float isFloat = CCUserDefault::sharedUserDefault()->getFloatForKey("float", 0.0f); int isInt = CCUserDefault::sharedUserDefault()->getIntegerForKey("int", 0); std::string isString = CCUserDefault::sharedUserDefault()->getStringForKey("string", ""); CCLOG("%b__%d__%f__%f__%i__%s",isBool,isDouble,isFloat,isInt,isString.c_str()); }else{ ttf->setString("load user data"); CCUserDefault::sharedUserDefault()->setBoolForKey("bool", true); CCUserDefault::sharedUserDefault()->setDoubleForKey("double", 998.0); CCUserDefault::sharedUserDefault()->setFloatForKey("float", 888.0f); CCUserDefault::sharedUserDefault()->setIntegerForKey("int", 88); CCUserDefault::sharedUserDefault()->setStringForKey("string", "Xff"); //提交 CCUserDefault::sharedUserDefault()->flush(); } }
生成文件 UserDefault.xml