Cocos2d-x 3.2Lua sample UserDefaultTest (User default configuration)

Source: Internet
Author: User

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:



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.