简体   繁体   English

OOP GUI错误:main.lua:4:尝试索引local(布尔值)...模块问题

[英]OOP GUI Error: main.lua:4: attempt to index local (a boolean value)… Issue with modules

Today, I was messing around with trying to make a gui class for game I'd like to start making in LOVE2D. 今天,我正在努力为游戏制作一个gui课程,我想开始在LOVE2D制作。 I decided to try and use OOP to make creating new menus easier in the future. 我决定尝试使用OOP,以便将来更轻松地创建新菜单。 The OOP works great until I try and put it into it's own module, where it gives me the error above. OOP工作得很好,直到我尝试把它放入它自己的模块,它给我上面的错误。 I've double and triple checked my code against similar code and I can't find the problem. 我已经对我的代码进行了两次和三次检查以防止类似的代码,我找不到问题。 I've also looked it up and there are similar threads, but nothing that helps my problem. 我也查了一下,有类似的线程,但没有什么能帮我解决问题。 Here is the relevant code... 这是相关代码......

From the main.lua 来自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"
}

And from the gui.lua... 而且来自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

I'm assuming the first snippet is Game/gui and that the second part is main.lua, if this is so, You are attempting to call .new() function which is clearly absent on your Game/gui file. 我假设第一个片段是Game / gui,第二个部分是main.lua,如果是这样,你试图调用.new()函数,这个函数在你的Game / gui文件中显然不存在。 You need to move all of gui's functions to it's own file as well, this includes gui.new() , gui:createDropDown and gui:drawGui() last but not least, you have to return gui at the end of it's own file. 你需要将所有gui的函数移动到它自己的文件中,这包括gui.new()gui:createDropDowngui:drawGui()最后但并非最不重要的是,你必须在它自己的文件末尾返回gui。

Your main file should end up somewhat like this: 您的主文件应该有点像这样:

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

and Game/gui somewhat like this: 和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

You see, you forgot to move its functions to its own file, and return gui itself. 你看,你忘了将它的功能移动到它自己的文件,并返回gui本身。 Don't forget to replace what i omitted on Game/gui! 别忘了更换我在Game / gui上省略的内容!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM