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

时间:2016-08-20 02:01:28

标签: oop module lua love2d

今天,我正在努力为游戏制作一个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

1 个答案:

答案 0 :(得分:0)

我假设第一个片段是Game / gui,第二个部分是main.lua,如果是这样,你试图调用.new()函数,这个函数在你的游戏中显然不存在/ 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上省略的东西!