Button 我开始在文件 main.lua 中使用其余代码创建一个类。一切正常,但现在我想拆分我的代码并创建一个 Button.lua 文件,如下所示:
\nButton = {}\n\nfunction Button:new(x, y, width, height, color, text, callback,arg1)\n local button = {\n x = x,\n y = y,\n width = width,\n height = height,\n color = color,\n text = text,\n callback = callback,\n arg1 = arg1,\n state = false,\n }\n setmetatable(button, self)\n self.__index = self\n return button\nend\n\nfunction Button:draw()\n local color = self.hover and {0.8, 0.8, 0.8} or self.color\n love.graphics.setColor(color)\n love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)\n if self.hover then\n love.graphics.setColor(1, 1, 1)\n love.graphics.rectangle("line", self.x-1, self.y-1, self.width+2, self.height+2)\n end\n if self.state == true then\n self.callback(self)\n end\n -- Affichage du texte\n love.graphics.setColor(0, 0, 0) -- couleur du texte noir\n local scale = self.height / 50\n local font = love.graphics.newFont(12*scale) \n love.graphics.setFont(font)\n love.graphics.printf(self.text, self.x, self.y + self.height / 2 - 10, self.width, "center")\nend\n\nfunction Button:update(dt)\n -- On r\xc3\xa9cup\xc3\xa8re la position de la souris\n local mouseX, mouseY = love.mouse.getPosition()\n -- On v\xc3\xa9rifie si la souris est dans la zone du bouton\n self.hover = mouseX > self.x and mouseX < self.x + self.width and mouseY > self.y and mouseY < self.y + self.height\n if self.hover and love.mouse.isDown(1) and self.state == false then\n self.state = true\n end\nend\nRun Code Online (Sandbox Code Playgroud)\n并像这样更改我的 main.lua 文件:
\n-- D\xc3\xa9finition de la classe Button\nlocal Button = require('button')\nlocal current_text = 1 -- Index du texte courant \xc3\xa0 afficher\nlocal boxstate = false\n\n\nfunction createTextbox(button)\n boxstate = true\n local text = button.arg1[current_text]\n local screen_width, screen_height = love.graphics.getDimensions()\n -- On v\xc3\xa9rifie si on est \xc3\xa0 la fin de la liste de textes\n if current_text == #button.arg1 then\n boxstate = false\n button.state = false\n current_text = 1\n return\n end\n -- On d\xc3\xa9finit la couleur verte\n love.graphics.setColor(0, 1, 0)\n -- On dessine le rectangle\n love.graphics.rectangle("fill", 0, screen_height - screen_height/4, screen_width, screen_height/4)\n -- On d\xc3\xa9finit la couleur noire pour le texte\n love.graphics.setColor(0, 0, 0)\n -- On dessine le texte\n love.graphics.print(text, 0, screen_height - screen_height/4)\n\n draw_arrow_moving()\n\n end\n\n local xa = love.graphics.getWidth()-20\nlocal ya = love.graphics.getHeight()\n-- amplitude du mouvement de la t\xc3\xaate de fl\xc3\xa8che (en pixels)\nlocal amplitude = 30\n-- vitesse de d\xc3\xa9placement de la t\xc3\xaate de fl\xc3\xa8che (en cycles par seconde)\nlocal speed = 1\n\nlocal texts = { "texte1", "texte2", "texte3","end" } -- Liste des textes \xc3\xa0 afficher\n\n-- fonction pour afficher une t\xc3\xaate de fl\xc3\xa8che mobile\nfunction draw_arrow_moving()\n -- d\xc3\xa9finition de la couleur rouge\n love.graphics.setColor(1, 0, 0)\n -- dessin de la t\xc3\xaate de fl\xc3\xa8che\n love.graphics.polygon("fill", xa, ya, xa+10, ya-10, xa, ya-20)\n end\n \n \n-- pour l'utiliser:\n-- cr\xc3\xa9ation d'un tableau de boutons\nbuttons = {}\n\n-- ajout de deux boutons au tableau\ntable.insert(buttons, Button:new(200, 10, 200, 100, {0, 0, 1}, "Mon bouton", createTextbox, texts)) -- bouton bleu avec texte "Bouton bleu"\n\nfunction love.load()\n -- largeur et hauteur de la fen\xc3\xaatre\n love.window.setFullscreen(true)\n -- titre de la fen\xc3\xaatre\n love.window.setTitle("Mon jeu")\n end \n\nfunction love.update(dt)\n for _, button in ipairs(buttons) do\n button:update()\n end \n -- met \xc3\xa0 jour la position de la t\xc3\xaate de fl\xc3\xa8che\n local t = love.timer.getTime()\n xa = (love.graphics.getWidth() - 70) + amplitude * math.sin(t * speed * 2 * math.pi)\n ya = love.graphics.getHeight() - 20\n\nend\n\nfunction love.mousepressed(x, y, button, istouch)\n if button == 1 then\n if x > 0 and x < love.graphics.getWidth() and y > love.graphics.getHeight() - love.graphics.getHeight()/4 and y < love.graphics.getHeight() and boxstate == true then\n current_text = current_text + 1\n if current_text > #texts then\n current_text = 1\n end\n end\n end\nend\n\n\nfunction love.draw()\n -- affichage de tous les boutons\n for _, button in ipairs(buttons) do\n button:draw()\n end\nend \nRun Code Online (Sandbox Code Playgroud)\n但我有这个
\nError: main.lua:54: attempt to index local 'Button' (a boolean value)\nstack traceback:\n [love "boot.lua"]:345: in function '__index'\n main.lua:54: in main chunk\n [C]: in function 'require'\n [love "boot.lua"]:316: in function <[love "boot.lua"]:126>\n [C]: in function 'xpcall'\n [love "boot.lua"]:355: in function <[love "boot.lua"]:348>\n [C]: in function 'xpcall'\nRun Code Online (Sandbox Code Playgroud)\n我不知道为什么
\n我已经尝试将我的两个代码合并到我的主代码中并且它有效
\n引用Lua 5.1参考手册:
如果加载程序没有返回值并且没有分配任何值
package.loaded[modname],则 require 分配true给该条目。无论如何,require返回 的最终值package.loaded[modname]。
这就是你得到布尔值的原因:你通过创建局部变量来需要模块Button,但是在button.lua模块中你忘记返回模块的内容:
local Button = {}
function Button:new(x, y, width, height, color, text, callback,arg1)
--
end
function Button:draw()
--
end
function Button:update(dt)
--
end
return Button
Run Code Online (Sandbox Code Playgroud)
PS:最好在Button本地声明