我正在Love2D制作平台游戏,我创建了自己的lua脚本来处理级别文件
我在dofile
和levelprocessor.lua
中使用了main.lua
,我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'
答案 0 :(得分:2)
替换
dofile( f )
通过
assert( love.filesystem.load( f ) )( )
love.filesystem.
*会查看源目录(以及其他一些地方),而不是工作目录。