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