Cocos2d-x 3.2示例UserDefaultTest(用户默认配置)
本篇博客介绍Cocos2d-x 3.2示例中的UserDefaulstTest,我们在开发中可能需要用到一些默认配置,一般会以xml形式保存。Cocos2d-x为我们提供了UserDefault类来实现这样的需求。
示例代码:
-- enable log
--[[
UserDefaultTest.lua
用户默认配置
]]--
local function doTest()
cclog("********************** init value ***********************")
-- set default value
-- 设置默认值
cc.UserDefault:getInstance():setStringForKey("string", "value1")-- 字符串
cc.UserDefault:getInstance():setIntegerForKey("integer", 10)-- 整型
cc.UserDefault:getInstance():setFloatForKey("float", 2.3)--浮点型
cc.UserDefault:getInstance():setDoubleForKey("double", 2.4)-- 双精度
cc.UserDefault:getInstance():setBoolForKey("bool", true)-- 布尔型
-- print value
-- 打印获取到的值
-- 根据key获取字符串值
local ret = cc.UserDefault:getInstance():getStringForKey("string")
cclog("string is %s", ret)
-- 根据key获取双精度值
local d = cc.UserDefault:getInstance():getDoubleForKey("double")
cclog("double is %f", d)
-- 根据key获取整型值
local i = cc.UserDefault:getInstance():getIntegerForKey("integer")
cclog("integer is %d", i)
-- 根据key获取浮点数值
local f = cc.UserDefault:getInstance():getFloatForKey("float")
cclog("float is %f", f)
-- 根据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
-- 修改值
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)
-- 刷新写入
cc.UserDefault:getInstance():flush()
-- print value
-- 根据key获取字符串值
local ret = cc.UserDefault:getInstance():getStringForKey("string")
cclog("string is %s", ret)
-- 根据key获取双精度值
local d = cc.UserDefault:getInstance():getDoubleForKey("double")
cclog("double is %f", d)
-- 根据key获取整型值
local i = cc.UserDefault:getInstance():getIntegerForKey("integer")
cclog("integer is %d", i)
-- 根据key获取浮点数值
local f = cc.UserDefault:getInstance():getFloatForKey("float")
cclog("float is %f", f)
-- 根据key获取布尔值
local b = cc.UserDefault:getInstance():getBoolForKey("bool")
if b == true then
cclog("bool is true")
else
cclog("bool is false")
end
end
function UserDefaultTestMain()
local ret = cc.Scene:create() -- 场景
local s = cc.Director:getInstance():getWinSize() -- 获取屏幕大小
local label = cc.Label:createWithTTF("UserDefault test see log", s_arialPath, 28)-- 创建标签
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 ret
end
日志消息如下: