Cocos2d-x 3.2Lua sample UserDefaultTest (User default configuration)
Cocos2d-x 3.2 Sample UserDefaultTest (User default configuration)
This blog introduces UserDefaulstTest in the example of Cocos2d-x 3.2. Some default configurations may be used in development and are generally saved in xml format. The Cocos2d-x provides us with the UserDefault class to fulfill this requirement.
Sample Code:
-- Enable log -- [[UserDefaultTest. lua user default configuration] -- local function doTest () cclog ("********************** init value ************* **********") -- set default value -- set the default value cc. userDefault: getInstance (): setStringForKey ("string", "value1") -- string cc. userDefault: getInstance (): setIntegerForKey ("integer", 10) -- integer cc. userDefault: getInstance (): setFloatForKey ("float", 2.3) -- floating point cc. userDefault: getInstance (): setDoubleForKey ("double", 2.4) -- double Precision cc. userDefault: getInstance (): setBoolForKey ("bool", true) -- Boolean -- print value -- print the obtained value -- Obtain the string value based on the key: local ret = cc. userDefault: getInstance (): getStringForKey ("string") cclog ("string is % s", ret) -- Obtain the double-precision value based on the key: local d = cc. userDefault: getInstance (): getDoubleForKey ("double") cclog ("double is % f", d) -- Obtain the integer value local I = cc based on the key. userDefault: getInstance (): getIntegerForKey ("integer") cclog ("integer is % d", I) -- get the floating point value local f = cc based on the key. userDefault: getInstance (): getFloatForKey ("float") cclog ("float is % f", f) -- obtain a Boolean value based on the key: local B = cc. userDefault: getInstance (): getBoolForKey ("bool") if B = true then cclog ("bool is true") else cclog ("bool is false") end -- cc. userDefault: getInstance (): flush () cclog ("********************** after change value ************ ***********") -- change the value -- modify the value of cc. userDefault: getInstance (): setStringForKey ("string", "value2") cc. userDefault: getInstance (): setIntegerForKey ("integer", 11) cc. userDefault: getInstance (): setFloatForKey ("float", 2.5) cc. userDefault: getInstance (): setDoubleForKey ("double", 2.6) cc. userDefault: getInstance (): setBoolForKey ("bool", false) -- refresh and write to cc. userDefault: getInstance (): flush () -- print value -- Obtain the string value local ret = cc based on the key. userDefault: getInstance (): getStringForKey ("string") cclog ("string is % s", ret) -- Obtain the double-precision value based on the key: local d = cc. userDefault: getInstance (): getDoubleForKey ("double") cclog ("double is % f", d) -- Obtain the integer value local I = cc based on the key. userDefault: getInstance (): getIntegerForKey ("integer") cclog ("integer is % d", I) -- get the floating point value local f = cc based on the key. userDefault: getInstance (): getFloatForKey ("float") cclog ("float is % f", f) -- obtain a Boolean value based on the key: local B = cc. userDefault: getInstance (): getBoolForKey ("bool") if B = true then cclog ("bool is true") else cclog ("bool is false") endfunction UserDefaultTestMain () local ret = cc. scene: create () -- Scenario local s = cc. director: getInstance (): getWinSize () -- Obtain the screen size. local label = cc. label: createWithTTF ("UserDefault test see log", s_arialPath, 28) -- create the label ret: addChild (label, 0) Label: setAnchorPoint (cc. p (0.5, 0.5) label: setPosition (cc. p (s. width/2, s. height-50) ret: addChild (CreateBackMenuItem () doTest () return retend
The log message is as follows: