我正在为一款游戏制作一个关卡加载系统。
function love.filedropped(file)
ofile=io.open(file:getFilename(),"r")
io.input(ofile)
file:close
levelc=io.read()
for i=1,levelc do
levels[i]=io.read()
print levels[i]
end
levelc应该是文件的第一行,file:getFilename是要打开的文件(包含路径)项目在启动时给出错误消息,我以前使用过类似的结构,但用于输出。错误在第30行,也就是levelc=io.read()。我试过改变文件指针的名称(它是"f"以前,现在" file"),我试过使用io.read("*l")而不是io.read(),但结果相同。编辑:-这是一个love. fileddrop(文件)-我需要从。txt打开其他文件以后,我真的不明白怎么做
love.filedropped
给出的参数是一个DroppedFile
在你的情况下,File:lines()
可能有帮助。
例如:
function love.filedropped(file)
-- Open for reading
file:open("r")
-- Iterate over the lines
local i = 0
for line in file:lines() do
i = i + 1
levels[i] = line
print(i, levels[i]) -- Notice the parentheses missing in your code
end
-- Close the file
file:close()
end
注意,love2d通常只允许读取/写入保存或工作目录中的文件。删除文件是例外。
与这个答案无关,但是我在你的代码中注意到的事情:
- 使用local,
oFile
应该是local - file:close()需要圆括号作为函数调用
- 打印 相同
filedropped
回调没有end
您也提到了读取其他文件,要这样做,您可以:
- 使用love. filessystem . newfile和类似的方法
- 推荐的一行love. filessystem .lines