OOP GUI Error: main.lua:4: attempt to index local (a boolean value)... Issue with modules
OOP GUI Error: main.lua:4: attempt to index local (a boolean value)... Issue with modules
今天,我正忙着为我想在 LOVE2D 中开始制作的游戏制作一个图形用户界面 class。我决定尝试使用 OOP 来使将来创建新菜单更容易。在我尝试将它放入它自己的模块之前,OOP 工作得很好,它给我上面的错误。我已经根据类似代码对我的代码进行了双重和三次检查,但我找不到问题所在。我也查了一下,有类似的线程,但没有任何帮助我的问题。这是相关代码...
来自main.lua
local gui = {
x = 0, y = 0,
width = 0, height = 0,
popupSpeed = 300,
active = false,
color = {red = 0, blue = 0, green = 0, alpha = 0},
menuType = "",
--buttons = require "Game/buttons"
}
并且从 gui.lua...
local newGUI = require "Game/gui"
local menus = {
playerInv = newGUI.new()
}
function love.load()
menus.playerInv:createDropDown("right" , _, 30, 100, love.graphics.getHeight() - 60, 50, 128, 50, 255)
end
function gui.new()
newMenu = {}
for k, v in pairs(gui) do
newMenu[k] = v
end
return newMenu
end
function gui:createDropDown(direction, x, y, width, height, red, blue, green, alpha)
self.red = red
self.blue = blue
self.green = green
self.alpha = alpha
if direction == "right" then
self.x = love.graphics.getWidth() - width
self.y = y
self.width = width
self.height = height
self.menuType = "rightDropDown"
elseif direction == "left" then
self.x = 0
self.y = y
self.widthMax = width
self.height = height
self.menuType = "leftDropDown"
elseif direction == "down" then
self.x = x
self.y = y
self.width = width
self.heightMax = height
self.menuType = "regDropDown"
end
end
function gui:drawGui()
if self.active == true then
love.graphics.setColor(self.red, self.blue, self.green, self.alpha)
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height, 10, 10, 6)
end
end
我假设第一个片段是 Game/gui,第二部分是 main.lua,如果是这样,您正试图调用 .new()
函数,但显然不存在在您的 Game/gui 文件中。您还需要将 gui 的所有功能移动到它自己的文件中,这包括 gui.new()
、gui:createDropDown
和 gui:drawGui()
最后但并非最不重要的是,您必须 return gui 在它自己的文件结束。
你的主文件应该是这样的:
local newGUI = require "Game/gui"
local menus = {
playerInv = newGUI.new()
}
function love.load()
menus.playerInv:createDropDown("right" , _, 30, 100, love.graphics.getHeight() - 60, 50, 128, 50, 255)
end
和Game/gui有点像这样:
local gui = {} -- With all the stuff inside
function gui.new()
-- With all the stuff it had inside
end
function gui:createDropDown()
-- With all the stuff it had inside
end
function gui:drawGui()
-- With all the stuff it had inside
end
return gui
你看,你忘了把它的功能移到它自己的文件中,return gui 本身。不要忘记替换我在 Game/gui!
上遗漏的内容
今天,我正忙着为我想在 LOVE2D 中开始制作的游戏制作一个图形用户界面 class。我决定尝试使用 OOP 来使将来创建新菜单更容易。在我尝试将它放入它自己的模块之前,OOP 工作得很好,它给我上面的错误。我已经根据类似代码对我的代码进行了双重和三次检查,但我找不到问题所在。我也查了一下,有类似的线程,但没有任何帮助我的问题。这是相关代码...
来自main.lua
local gui = {
x = 0, y = 0,
width = 0, height = 0,
popupSpeed = 300,
active = false,
color = {red = 0, blue = 0, green = 0, alpha = 0},
menuType = "",
--buttons = require "Game/buttons"
}
并且从 gui.lua...
local newGUI = require "Game/gui"
local menus = {
playerInv = newGUI.new()
}
function love.load()
menus.playerInv:createDropDown("right" , _, 30, 100, love.graphics.getHeight() - 60, 50, 128, 50, 255)
end
function gui.new()
newMenu = {}
for k, v in pairs(gui) do
newMenu[k] = v
end
return newMenu
end
function gui:createDropDown(direction, x, y, width, height, red, blue, green, alpha)
self.red = red
self.blue = blue
self.green = green
self.alpha = alpha
if direction == "right" then
self.x = love.graphics.getWidth() - width
self.y = y
self.width = width
self.height = height
self.menuType = "rightDropDown"
elseif direction == "left" then
self.x = 0
self.y = y
self.widthMax = width
self.height = height
self.menuType = "leftDropDown"
elseif direction == "down" then
self.x = x
self.y = y
self.width = width
self.heightMax = height
self.menuType = "regDropDown"
end
end
function gui:drawGui()
if self.active == true then
love.graphics.setColor(self.red, self.blue, self.green, self.alpha)
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height, 10, 10, 6)
end
end
我假设第一个片段是 Game/gui,第二部分是 main.lua,如果是这样,您正试图调用 .new()
函数,但显然不存在在您的 Game/gui 文件中。您还需要将 gui 的所有功能移动到它自己的文件中,这包括 gui.new()
、gui:createDropDown
和 gui:drawGui()
最后但并非最不重要的是,您必须 return gui 在它自己的文件结束。
你的主文件应该是这样的:
local newGUI = require "Game/gui"
local menus = {
playerInv = newGUI.new()
}
function love.load()
menus.playerInv:createDropDown("right" , _, 30, 100, love.graphics.getHeight() - 60, 50, 128, 50, 255)
end
和Game/gui有点像这样:
local gui = {} -- With all the stuff inside
function gui.new()
-- With all the stuff it had inside
end
function gui:createDropDown()
-- With all the stuff it had inside
end
function gui:drawGui()
-- With all the stuff it had inside
end
return gui
你看,你忘了把它的功能移到它自己的文件中,return gui 本身。不要忘记替换我在 Game/gui!
上遗漏的内容