文件不存在,但实际上存在。

我在Love2D中制作平台游戏,并创建了自己的lua脚本来处理关卡文件。

levelprocessor.luamain.lua中我使用了 dofilerequire() 成功加载了模块。

但是,当我尝试处理关卡(level1.lua)时,它说找不到该文件。但是,该文件与levelprocessor.luamain.lua在同一目录中。

以下是我的代码:

levelprocesser.lua:

local blocks = {
["1"]=(love.graphics.newImage("blocks/grassdirt/grass.png")) -- singular piece of grass
}

local mod={
}

function mod.placeBlock(blockID, posx, posy)
position = {x=posx*32,y=posy*32}; --blocks are 16x16 and drawn 32x32, and x and y here are in block coordinates,
-- so a block with ID 1, posx 1, and posy=1, would be a grass block 32 pixels x and 32 pixels y.
for key, value in ipairs(blocks) do
-- glad roblox and that book called Beginning Lua Programming made me understand generic for loops
if key == blockID then
love.graphics.draw(key, position.x, position.y, 2,2);
else
error("Error in processing level file")
end
end
end

function mod.processFile(fileDir)
assert(type(fileDir) == "string", "error in function processFile in file levelprocessor.lua: argument 1 must be ")
local level = dofile(fileDir);
for i,v in ipairs(level) do
placeBlock(v.blockID, v.posx, v.posy)
end
end
return mod

main.lua:

levelprocessor = require "levelprocessor" -- remember kids, do require without file extension
function love.load()
healthbar = love.graphics.newImage("ui/Untitled.png")
good_health = love.graphics.newQuad(0, 0, 100, 36, healthbar:getDimensions())
caution_health = love.graphics.newQuad(100,36,102, 36, healthbar:getDimensions())
warning_health = love.graphics.newQuad(102,36,204,36,healthbar:getDimensions())
grass_top_singular = love.graphics.newImage("blocks/grassdirt/grass.png")
 end

function love.draw()
    love.graphics.draw(grass_top_singular, 0,568,0,2,2); -- 600-32=558... mental math, so im not sure if it is truly correct
    levelprocessor.processFile("level1.lua")
end

错误本身是:

cannot open level1.lua: No such file or directory

Traceback

[C]: in function 'dofile'
levelprocessor.lua:22: in function 'processFile'
main.lua:12 in function 'draw'
[C]: in function 'xpcall'
点赞
用户805875
用户805875

将下面翻译成中文并且保留原本的 markdown 格式,

dofile( f )

替换为

assert( love.filesystem.load( f ) )( )

love.filesystem.\* 将在源目录(和其他几个地方)中查找,而不是在工作目录中。

2015-12-03 20:19:13