Cocos2d-x 3.2Lua示例UserDefaultTest(用户默认配置)

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


日志消息如下:



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小巫技术博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值