![](/img/trans.png)
[英]attempt to call method 'print' (a nil value) when implementation OOP in Lua
[英]OOP GUI Error: main.lua:4: attempt to index local (a boolean value)… Issue with modules
今天,我正在努力为游戏制作一个gui课程,我想开始在LOVE2D制作。 我决定尝试使用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()
最后但并非最不重要的是,你必须在它自己的文件末尾返回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
你看,你忘了将它的功能移动到它自己的文件,并返回gui本身。 别忘了更换我在Game / gui上省略的内容!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.