簡體   English   中英

OOP GUI錯誤:main.lua:4:嘗試索引local(布爾值)...模塊問題

[英]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: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上省略的內容!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM