2012-12-12 18 views
0

嗨我正在使用corona sdk構建應用程序,這是一個交互式電子書。我需要做的是保存當前變量,然後在啓動時加載一個包含用戶所在場景以及已經做出的選擇的外部文件。我將所有這些存儲爲全局變量,每個選項的值都是0或1,場景名稱存儲在數組中,指定的編號即。場景1 = 1。任何幫助將令人難以置信的讚賞。提前致謝。使用json返回變量的corona sdk狀態保存

回答

0

創建一個包含要存儲變量的表格,並使用這兩個函數將表格保存到文件中並讀取文件並返回存儲的表格。

local function write(table, fileName) 
    local filePath = system.pathForFile(fileName, system.DocumentsDirectory) 
    local file = io.open(filePath, "w") 
    local encodedTable = json.encode(table) 
    file:write(encodedTable) 
    io.close(file) 
end 

local function read(fileName) 
    local filePath = system.pathForFile(fileName, system.DocumentsDirectory) 
    local file = io.open(filePath, "r") 

    if file then 
     local contents = file:read("*all") 
     local decodedTable = json.decode(contents) 

     io.close(file) 
     return decodedTable 
    else 
     return false 
    end 
end 
相關問題