0
我想要的只是保存我的用户(玩家)高分,并且这个信息在应用程序(游戏)之间持续存在于Corona SDK(Lua)中。我确实希望它在iOS和Android上很好地工作。我的高分数据实际上是两个包含数字的lua表。如何实现Corona中的“NSUserDefaults”功能?
什么是正确和最简单的方法呢?
我想要的只是保存我的用户(玩家)高分,并且这个信息在应用程序(游戏)之间持续存在于Corona SDK(Lua)中。我确实希望它在iOS和Android上很好地工作。我的高分数据实际上是两个包含数字的lua表。如何实现Corona中的“NSUserDefaults”功能?
什么是正确和最简单的方法呢?
您可以保存得分到一个表,然后将其序列化为JSON格式的文本文件。
local json=require("json")
local savefile="scores.json"
scores=
{
{
level=1,
status=0,
highscore=0,
},
{
level=2,
status=0,
highscore=0,
},
}
function getScore(filename, base)
-- set default base dir if none specified
if not base then
base = system.DocumentsDirectory
end
-- create a file path for corona i/o
local path = system.pathForFile(filename, base)
-- will hold contents of file
local contents
-- io.open opens a file at path. returns nil if no file found
local file = io.open(path, "r")
local scores
if file then
-- read all contents of file into a string
contents = file:read("*a")
if content ~= nil then
scores=json.decode(content)
end
io.close(file) -- close the file after using it
end
return scores
end
function saveScore(filename, base)
-- set default base dir if none specified
if not base then
base = system.DocumentsDirectory
end
-- create a file path for corona i/o
local path = system.pathForFile(filename, base)
-- io.open opens a file at path. returns nil if no file found
local file = io.open(path, "wb")
if file then
-- write all contents of file into a string
file:write(json.encode(scores))
io.close(file) -- close the file after using it
end
end
全局变量scores
可以操纵像一个正常的表,当你要加载或保存scores
表中可以拨打以上功能。
请注意,如果你要使用游戏中心或其他在线排行榜服务,你需要加密这个数据库或用户越狱的手机可以编辑该表显示任何他们想要的分数,然后重新打开应用程序,人为地提高他们的在线评分。 – 2012-02-16 01:32:51
cctan:太好了,非常感谢!正是我在寻找和超越。 杰克:谢谢你的提示,我不知道that.Will当我实现游戏中心做。 – mindbomb 2012-02-16 13:15:42
@JackLawrence +1提的加密 – cctan 2012-02-17 00:55:19