2013-04-07 16:20:43 +08:00
|
|
|
-- enable log
|
|
|
|
|
|
|
|
|
|
|
|
local function doTest()
|
|
|
|
cclog("********************** init value ***********************")
|
|
|
|
|
|
|
|
-- set default value
|
|
|
|
|
2013-08-20 13:44:37 +08:00
|
|
|
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)
|
2013-04-07 16:20:43 +08:00
|
|
|
|
|
|
|
-- print value
|
|
|
|
|
2013-08-20 13:44:37 +08:00
|
|
|
local ret = cc.UserDefault:getInstance():getStringForKey("string")
|
2013-04-07 16:20:43 +08:00
|
|
|
cclog("string is %s", ret)
|
|
|
|
|
2013-08-20 13:44:37 +08:00
|
|
|
local d = cc.UserDefault:getInstance():getDoubleForKey("double")
|
2013-04-07 16:20:43 +08:00
|
|
|
cclog("double is %f", d)
|
|
|
|
|
2013-08-20 13:44:37 +08:00
|
|
|
local i = cc.UserDefault:getInstance():getIntegerForKey("integer")
|
2013-04-07 16:20:43 +08:00
|
|
|
cclog("integer is %d", i)
|
|
|
|
|
2013-08-20 13:44:37 +08:00
|
|
|
local f = cc.UserDefault:getInstance():getFloatForKey("float")
|
2013-04-07 16:20:43 +08:00
|
|
|
cclog("float is %f", f)
|
|
|
|
|
2013-08-20 13:44:37 +08:00
|
|
|
local b = cc.UserDefault:getInstance():getBoolForKey("bool")
|
2013-04-07 16:20:43 +08:00
|
|
|
if b == true then
|
|
|
|
cclog("bool is true")
|
|
|
|
else
|
|
|
|
cclog("bool is false")
|
|
|
|
end
|
|
|
|
|
2013-08-20 13:44:37 +08:00
|
|
|
--cc.UserDefault:getInstance():flush()
|
2013-04-07 16:20:43 +08:00
|
|
|
|
|
|
|
cclog("********************** after change value ***********************")
|
|
|
|
|
|
|
|
-- change the value
|
|
|
|
|
2013-08-20 13:44:37 +08:00
|
|
|
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)
|
2013-04-07 16:20:43 +08:00
|
|
|
|
2013-08-20 13:44:37 +08:00
|
|
|
cc.UserDefault:getInstance():flush()
|
2013-04-07 16:20:43 +08:00
|
|
|
|
|
|
|
-- print value
|
|
|
|
|
2013-08-20 13:44:37 +08:00
|
|
|
ret = cc.UserDefault:getInstance():getStringForKey("string")
|
2013-04-07 16:20:43 +08:00
|
|
|
cclog("string is %s", ret)
|
|
|
|
|
2013-08-20 13:44:37 +08:00
|
|
|
d = cc.UserDefault:getInstance():getDoubleForKey("double")
|
2013-04-07 16:20:43 +08:00
|
|
|
cclog("double is %f", d)
|
|
|
|
|
2013-08-20 13:44:37 +08:00
|
|
|
i = cc.UserDefault:getInstance():getIntegerForKey("integer")
|
2013-04-07 16:20:43 +08:00
|
|
|
cclog("integer is %d", i)
|
|
|
|
|
2013-08-20 13:44:37 +08:00
|
|
|
f = cc.UserDefault:getInstance():getFloatForKey("float")
|
2013-04-07 16:20:43 +08:00
|
|
|
cclog("float is %f", f)
|
|
|
|
|
2013-08-20 13:44:37 +08:00
|
|
|
b = cc.UserDefault:getInstance():getBoolForKey("bool")
|
2013-04-07 16:20:43 +08:00
|
|
|
if b == true then
|
|
|
|
cclog("bool is true")
|
|
|
|
else
|
|
|
|
cclog("bool is false")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function UserDefaultTestMain()
|
2013-08-20 13:44:37 +08:00
|
|
|
local ret = cc.Scene:create()
|
|
|
|
local s = cc.Director:getInstance():getWinSize()
|
|
|
|
local label = cc.LabelTTF:create("UserDefault test see log", "Arial", 28)
|
2013-04-07 16:20:43 +08:00
|
|
|
ret:addChild(label, 0)
|
2013-08-20 13:44:37 +08:00
|
|
|
label:setPosition( cc.p(s.width/2, s.height-50) )
|
2013-11-18 22:16:49 +08:00
|
|
|
ret:addChild(CreateBackMenuItem())
|
2013-04-07 16:20:43 +08:00
|
|
|
doTest()
|
|
|
|
return ret
|
|
|
|
end
|