OOP GUI 错误:main.lua:4: 尝试索引本地 (一个布尔值)...与模块有问题

今天我在尝试为我想要开始制作的LOVE2D游戏创建一个gui类。 我决定尝试使用OOP来使未来创建新菜单更容易。OOP工作得很好,直到我尝试将其放入它自己的模块中,它给我上面的错误。我已经多次检查了我的代码,与类似的代码进行了双倍和三倍的检查,但我找不到问题。我也查了一下,有类似的帖子,但没有对我的问题有帮助的东西。以下是相关的代码...

来自主.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
 
点赞
用户1381216
用户1381216

我假设第一段代码是游戏/gui部分,第二部分是main.lua。如果是这样,您正在尝试调用.new()函数,但这个函数在您的Game/gui文件中显然不存在。您需要将gui的所有函数移动到它自己的文件中,包括gui.new()gui:createDropDowngui: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中省略的内容!

2016-08-20 06:09:05