【发布时间】:2014-09-02 19:43:36
【问题描述】:
我正在创建游戏,需要将游戏数据写入文件。如果文件不存在,我让游戏创建文件并读取文件的内容(我手动输入),但我无法让它写入文件。
local path = system.pathForFile("gameData.gameData", system.DocumentsDirectory)
local myFile
defaultGameData = "It Worked"
if (path) then
myFile = io.open(path, "r")
end
if(myFile) then
print('file')
else
myFile:close()
--io.close(myFile)
myFile = io.open(path, 'w')
myFile:write( "My Test" )
io.close(myFile)
end
myFile = nil
那部分有效。然后我移动到下一个场景并尝试写一些新的东西
local saveData = "My app state data"
local path = system.pathForFile("gameData.gameData", system.DocumentsDirectory)
local myfile = io.open( path, "w" )
myfile:write( saveData )
io.close( myfile )
但得到错误
mainMenu.lua:43: 尝试索引本地“myfile”(一个 nil 值)
我知道该文件在沙盒中,并且此代码是从 corona 文档中复制的。我究竟做错了什么???
【问题讨论】:
-
local myfile, err = io.open( path, "w" )然后print(err)看看你遇到了什么错误。 -
权限被拒绝。所以该文件是由应用程序创建的。此错误发生在模拟器中。我还没有在手机上测试过。