【发布时间】:2016-04-29 19:49:07
【问题描述】:
最近我一直在尝试学习如何编写游戏代码,我使用 lua 5.1 作为语言,使用 love2d 作为引擎。我对这两个都不熟悉,我仍在尝试学习如何使用它们,所以所有这些都是基于 Goature 在 youtube 上的教程的示例代码。这是程序的菜单部分,我得到“states/menu/main.lua:22: bad argument #1 to 'draw'(Drawable expected, got nil)”。我知道问题出在表格或 drawButton 函数中的参数上,但是我不知道问题是什么或如何解决它。 如果有人能解释什么是错的,那就太好了。谢谢!
function load()
love.graphics.setBackgroundColor(190, 190, 190, 255)
imgPlay = love.graphics.newImage("Textures/start.png")
imgPlayOn = love.graphics.newImage("Textures/start_on.png")
imgexit = love.graphics.newImage("Textures/exit.png")
imgexitOn = love.graphics.newImage("Textures/exit_on.png")
end
buttons = {{imgOff = imgPlay, imgOn = imgPlayOn, x = 400, y = 300 - 64, w = 256, h = 64, action = "play"},
{imgOff = imgexit, imgOn = imgexitOn, x = 400, y = 300 + 64, w = 256, h = 64, action ="exit"}
}
local function drawButton(highlightOff, highlightOn, x, y, w, h, mx, my)
local ins = insideBox(mx, my, x - (w/2), y - (h/2), w, h)
love.graphics.setColor(255, 255, 255, 255)
if ins then
love.graphics.draw(highlightOn, x, y, 0, 1, 1, (w/2), (h/2))
else
love.graphics.draw(highlightOff, x, y, 0, 1, 1, (w/2), (h/2))
end
end
function love.update(dt)
end
function love.draw()
local x = love.mouse.getX()
local y = love.mouse.getY()
for k, v in pairs (buttons) do -- v acts as an "address"
drawButton(v.imgOff, v.imgOn, v.x, v.y, v.w, v.h, x, y) -- each elemant corresponds in the table
end
end
【问题讨论】: