如何在corona SDK中从文件中读取或获取值?

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

我使用了一个叫做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")
点赞
用户7026995
用户7026995

我推荐你使用Corona-SDK的简单表格加载和保存功能 - 这是两个非常简单的加载和保存函数,用于存储Lua表格并将其读取回来。 需要使用Corona SDK JSON库。

2017-06-16 12:53:58