[英]Save table in corona SDK
美好的一天,有一段時間我試圖將表保存在Corona SDK中。 退出和進入游戲時,仍必須將解鎖的級別解鎖。
我已經嘗試了幾件事,但仍然無法弄清楚。 我該怎么做?
這是我的代碼的一部分:
G = {
Gamescore1=0,
Gamescore2=0,
Gamescore3=0,
Gamescore4=0,
Gamescore5=0,
Gamescore6=0,
Gamescore7=0,
Gamescore8=0,
Gamescore9=0,
}
--Gamescore1=0
function addscore1()
G.Gamescore1=G.Gamescore1+1
-----------------------------------------------------------
end
----------------------------------------------------------
function addscore2()
G.Gamescore2=G.Gamescore2+1
end
---------------------------
function addscore3()
G.Gamescore3=G.Gamescore3+1
end
function addscore4()
G.Gamescore4=G.Gamescore4+1
end
function addscore5()
G.Gamescore5=G.Gamescore5+1
end
Gamescore6=0
function addscore6()
G.Gamescore6=G.Gamescore6+1
end
function addscore7()
G.Gamescore7=G.Gamescore7+1
end
function addscore8()
G.Gamescore8=G.Gamescore8+1
end
function addscore9()
G.Gamescore9=G.Gamescore9+1
end
注意:如果Gamescore = 1,Gamescore變量將進入下一個級別,下一級別將被解鎖。
因此,每次激活addscore功能時,我都需要保存新的Gamescore。
例如:Gamescore1 = 0
觸發了addscore1函數,因此現在Gamescore1 =1。如果Gamescore1 = 1,則下一個級別被解鎖。 現在,我只需要用新值(1)保存Gamescore1。
我希望這是足夠的信息。 如果有人可以幫助我,我將不勝感激。
提前致謝!
程序結束時,表值將丟失。 您必須將表保存到文件或數據庫。
為此,您必須以某種方式序列化表,因為您無法直接保存表。 有無數種方法,因此在這里我將不做過多解釋。 只需在網絡上搜索Lua表序列化或閱讀以下內容: http : //lua-users.org/wiki/TableSerialization
基本上,您將表的內容轉換為可以寫入文件或從文件讀取的內容。
文件內容可能如下所示:
Gamescore1 = 1; Gamescore2 = 0; 依此類推,如果它只是0和1,則可以簡單地存儲一些二進制表示。
選擇適合您需求的任何東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.