【问题标题】:corona sdk state saving using json to return variablescorona sdk状态保存使用json返回变量
【发布时间】:2012-12-12 14:51:33
【问题描述】:

您好,我正在使用 corona sdk 构建一个应用程序,这是一本交互式电子书。我需要做的是保存当前变量,然后在启动时加载一个外部文件,其中包含用户所在的场景以及已经做出的选择。我将所有这些存储为全局变量,每个选项都被赋予一个 0 或 1 的值,场景名称存储在一个具有指定数字的数组中,即。数组中的场景 1 = 1。任何帮助将不胜感激。提前致谢。

【问题讨论】:

    标签: json coronasdk read-write state-saving


    【解决方案1】:

    使用您要存储的变量创建一个表,并使用这两个函数将表保存在文件中并读取文件并返回存储的表。

    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
    

    【讨论】:

      猜你喜欢
      • 2015-09-18
      • 1970-01-01
      • 2017-09-27
      • 2012-08-23
      • 1970-01-01
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      相关资源
      最近更新 更多