Lua 我有一个加载文件错误。卢阿
我试图在我的游戏中实现highscore系统,但是当我试图声明highscore加载文件时,我得到了这个错误Lua 我有一个加载文件错误。卢阿,lua,coronasdk,Lua,Coronasdk,我试图在我的游戏中实现highscore系统,但是当我试图声明highscore加载文件时,我得到了这个错误 Attempt to call global 'loadFile' (a nil value) 这是我的密码 highscore = loadFile ("highscore.txt") local function checkForFile () if highscore == "empty" then highscore = 0 saveF
Attempt to call global 'loadFile' (a nil value)
这是我的密码
highscore = loadFile ("highscore.txt")
local function checkForFile ()
if highscore == "empty" then
highscore = 0
saveFile("highscore.txt", highscore)
end
end
checkForFile()
print ("Highscore is", highscore)
local function onSystemEvent ()
if playerScore > tonumber(highscore) then
--We use tonumber as highscore is a string when loaded
saveFile("highscore.txt", score)
end
end
Runtime:addEventListener( "system", onSystemEvent )
我正在使用Corona SDK。Corona ask的开发人员发布了一篇关于保存和写入文件的文章,这应该可以满足您的需要 基本上,您可以通过system.pathForFile获取路径,然后使用io.open打开它 你可以这样做:
local path = system.pathForFile( "highscore.txt", system.DocumentsDirectory )
local file = io.open(path, 'w+')
然后,要获取文件的内容:
local content = file:read('*a')
local highscore
if (content ~= null)
highscore = tonumber(content)
// do stuff with the loaded highscore
end
file:write(highscore)
要写入文件,请执行以下操作:
local content = file:read('*a')
local highscore
if (content ~= null)
highscore = tonumber(content)
// do stuff with the loaded highscore
end
file:write(highscore)
corona ask的开发人员发布了一篇关于保存和写入文件的好文章,应该可以满足您的需要 基本上,您可以通过system.pathForFile获取路径,然后使用io.open打开它 你可以这样做:
local path = system.pathForFile( "highscore.txt", system.DocumentsDirectory )
local file = io.open(path, 'w+')
然后,要获取文件的内容:
local content = file:read('*a')
local highscore
if (content ~= null)
highscore = tonumber(content)
// do stuff with the loaded highscore
end
file:write(highscore)
要写入文件,请执行以下操作:
local content = file:read('*a')
local highscore
if (content ~= null)
highscore = tonumber(content)
// do stuff with the loaded highscore
end
file:write(highscore)
正在加载的文件不是Lua文件,而是文本文件。因此,即使loadfile存在,也没有必要使用它。将io.read与file:read或file:line一起使用,其中file是io.open返回的对象。正在加载的文件不是Lua文件,而是文本文件。因此,即使loadfile存在,也没有必要使用它。将io.read与file:read或file:line一起使用,其中file是io.open返回的对象。这意味着函数loadFile不存在。另外,请缩进代码。函数loadFile不存在。你介意给我们一些你正在使用的引擎的信息吗?我正在使用corona SDK。也非常抱歉的非缩进我是新的论坛张贴!如何创建loadFile?这意味着loadFile函数不存在。另外,请缩进代码。函数loadFile不存在。你介意给我们一些你正在使用的引擎的信息吗?我正在使用corona SDK。也非常抱歉的非缩进我是新的论坛张贴!如何创建加载文件?