2017-06-16 92 views
0

我目前正在制作corona SDK中的应用程序。我现在的目标是创建可以存储在.txt文件中的内容(如字符串或布尔值)。我想要做的是在一个,让我们说例如scores.lua文件具有所有的值,然后,当需要在main.lua文件中使用它们。问题是main.lua没有得到我保存在scores.lua中的文件。如何从corona SDK中的文件读取/获取值?

我使用一种叫做ego.lua

function saveFile(fileName, fileData) 
    local path = system.pathForFile(fileName, system.DocumentsDirectory) 
    local file = io.open(path, "w+") 


if file then 
    file:write(fileData) 
    io.close(file) 
end 
end 


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


if file then 
    local fileData = file:read("*a") 
    io.close(file) 
    return fileData 
else 
    file = io.open(path, "w") 
    file:write("empty") 
    io.close(file) 
    return "empty" 
    end 
end 

和我保存在我的main.lua文件:

ego = require "ego" 
saveFile = ego.saveFile 
loadFile = ego.loadFile 

valueName = loadFile("gucci.txt") 
local money = display.newText(tostring(valueName), 200, 100, "Helvetica", 20) 

和我score.lua文件:

ego = require "ego" 
saveFile = ego.saveFile 
loadFile = ego.loadFile 

saveFile("gucci.txt", "This works") 

回答