如何创建按钮 API
2017-1-9 20:50:24
收藏:0
阅读:123
评论:1
我正在尝试使用ComputerCraft中的Lua制作一个易于使用的按钮API,但遇到了一些麻烦。当我执行以下操作时:
os.loadAPI("button")
action=function()
term.clear()
term.setCursorPos(1,1)
print("Hello!")
end
button.newButton("B1",5,5,20,10)
button.drawButton("B1",colors.orange,colors.white)
button.onClick("B1",action,true)
什么都没有发生,甚至没有绘制颜色。我进行了测试,当我将像colors.white这样的内容存储为变量,然后打印变量时,它会返回该颜色的数字代码,这来自于colors API。这是我的代码:
--要使用newButton函数,执行以下操作:
--button.newButton(exampleButton)
--要使用onClick函数,请按如下方式创建变量:
--exampleFunc=function()
--(代码)
--end
--然后使用相同的变量调用onClick:
--button.onClick(exampleButton,exampleFunc)
buttons={}
xPos=0
yPos=0
function removeButton(buttonName)
for key, fields in pairs(buttons) do
if key == buttonName then
table.remove(button,buttonName)
else
print("ERROR: button name not available")
end
end
end
function onClick(buttonName,action,boolean)
for key, fields in pairs(buttons) do
if boolean then
testClick(action)
end
end
end
function drawSeparateButton(x,y,w,h,outLineColor,fillColor)
if key == buttonName then
x=buttons[buttonName]["x"]
y=buttons[buttonName]["y"]
w=buttons[buttonName]["w"]
h=buttons[buttonName]["h"]
paintutils.drawBox(x,y,x+(w-1),y+(h-1),outLineColor)
paintutils.drawFilledBox(x+1,y+1,x+(w-2),y+(h-2),fillColor)
end
end
function testClick(action)
for key, fields in ipairs(buttons) do
x=buttons[buttonName]["x"]
y=buttons[buttonName]["y"]
w=buttons[buttonName]["w"]
h=buttons[buttonName]["h"]
x2=x+(w-1)
y2=y+(h-1)
button,xPos,yPos=os.pullEvent("mouse_click")
if xPos>=x and xPos<=x2 and yPos>=y and yPos<=y2 then
action()
end
end
end
function newButton(buttonName,X,Y,W,H)
buttons[buttonName] = {x=X,y=Y,w=W,h=H}
end
function drawButton(buttonName,outLineColor,fillColor)
for key, fields in ipairs(buttons) do
if key == buttonName then
x=buttons[buttonName]["x"]
y=buttons[buttonName]["y"]
w=buttons[buttonName]["w"]
h=buttons[buttonName]["h"]
x2=x+w-1
y2=y+h-1
x3=x+1
y3=y+1
x4=x+w-2
y4=y+h-2
paintutils.drawBox(x,y,x2,y2,outLineColor)
paintutils.drawFilledBox(x3,y3,x4,y4,fillColor)
elseif key ~= buttonName then
print("Button name not availabel")
end
end
end
我只需要能够将像colors.white这样的颜色存储为变量并将其返回为colors.white,而不是颜色代码。我还需要能够检查哪个按钮被点击并运行用户指定的函数。
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

我将浏览你的原型代码并指出我看到的一些错误,也会尝试回答你的问题。我会假设你想在一个表格中设置一个键值并从外部访问它。
对于你的问题,一个快速简短的答案是你可以将表格嵌套在表格中,并通过键或索引访问它们。但是,我会提出一个设计更改,将
exampleFunc存储为每个按钮表格的成员,以将其与特定的按钮相关联。例如:
buttons = {} buttons.playButton = {x=0, y=0, w=10, h=10, func=function() return end} buttons.quitButton = {x=0, y=30, w=10, h=10, func=function() return end} ... buttons.quitButton.x = 10 buttons.playButton.func()表格具有键值结构,其中键可以是字符串或数字。根据键的数据类型,有多种访问数组的方法。
例如,我们可以写成
buttons.quitButton.x = 10,也可以写成buttons["quitButton"].x = 10或buttons["quitButton"]["x"] = 10或buttons.quitButton["x"] = 10。这个页面 是学习 Lua 表格的一个很好的起点。
根据这个页面,
os.pullEvent()是阻塞的,你只能每次鼠标点击检查一个按钮是否被点击。考虑循环遍历你的buttons表格,检查每个按钮是否落在它的矩形范围内。一旦找到鼠标点击的按钮,就可以调用它的func成员。在我们讨论这种方法时,while true do循环是完全不必要的。function removeButton(buttonName) for buttonName in pairs(button) do ... function newButton(buttonName) state=true for buttonName in pairs(buttons) do ...你可能来自 Python 背景,其中存在
if element in list语句,但 Lua 没有这样的语句。你使用的for循环正在循环遍历列表的每个成员。你也没有捕获 pairs() 函数返回的所有变量。解决这个问题的方法可能如下:function buttonFunction(buttonName) for key, fields in pairs(buttons) do if key == buttonName then ... end end有多个实例引用变量
button,但实际上要引用的是buttons。