love2d - 文件不存在但确实存在
love2d - file doesnt exist but it does
我正在 Love2D 中制作平台游戏,我创建了自己的 lua 脚本来处理关卡文件。
我在 levelprocessor.lua
和 main.lua
中使用了 dofile
,我 require()
它和模块成功加载。
但是,当我尝试处理关卡 (level1.lua
) 时,它说找不到文件。但该文件存在于与 levelprocessor.lua
和 main.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'
替换
dofile( f )
来自
assert( love.filesystem.load( f ) )( )
love.filesystem.
* 将在源目录(和其他几个地方)中查找,而不是在工作目录中查找。
我正在 Love2D 中制作平台游戏,我创建了自己的 lua 脚本来处理关卡文件。
我在 levelprocessor.lua
和 main.lua
中使用了 dofile
,我 require()
它和模块成功加载。
但是,当我尝试处理关卡 (level1.lua
) 时,它说找不到文件。但该文件存在于与 levelprocessor.lua
和 main.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'
替换
dofile( f )
来自
assert( love.filesystem.load( f ) )( )
love.filesystem.
* 将在源目录(和其他几个地方)中查找,而不是在工作目录中查找。