遇到一些Computercraft/Lua代码的问题。

嗨,我想让我的 Computercraft 中的 Lua 代码允许用户通过右键单击顶部的监视器来打开/关闭红石信号,但我无法让它正常工作。

monitor = peripheral.wrap("top")
monitor.clear()
monitor.setTextColor(colors.red)
monitor.setCursorPos(1,1)
monitor.setTextScale(1)
monitor.write("Hello")

function rubber()
    monitor.setCursorPos(1,2)
    monitor.clearLine()

    if rs.getOutput("right",true) then
        monitor.write("Rubber farm is on")
    elseif rs.getOutput("right",false) then
        monitor.write("Rubber farm is off")
    end

    local event = {os.pullEvent()}

    if event == "monitor_touch" then
        if rs.getOutput("right") == true then
            rs.setOutput("right",false)
        else
            rs.setOutput("right",true)
        end
    else
        write("test")
    end

    rubber()
end

现在它只显示“Hello”,我不知道该如何修复,有人知道吗?而且我是 Lua 的初学者,我可能会犯一些很简单的错误。谢谢。

点赞
用户102441
用户102441
本地变量 `event` 用于存储一个元组,它来自于 `os.pullEvent()` 方法。在你的代码中,你将这个元组组合成了一个表。这没有问题,但是你接着将这个表与一个字符串进行了比较。表不能等于字符串,它们是两个不同的类型。你需要将表还原为其包含的元组,然后取出第一个元素来进行比较,或者直接将第一个元素赋值给 `event`:

local event = os.pullEvent() if event == "monitor_touch" then


或者在比较的时候取出元组的第一个元素:

local event = { os.pullEvent() } if event[1] == "monitor_touch" then

```

2014-04-06 16:49:44
用户3492749
用户3492749

你需要调用这个函数。

rubber()

2014-04-09 09:58:27
用户3820393
用户3820393

你需要关闭你的函数

function rubber()

  monitor.setCursorPos(1,1)

  monitor.clearLine()
end

end是你需要加入的这个小词。

2014-07-09 12:24:29
用户3189167
用户3189167

问题是你想要让那个函数无限循环,但你没有在函数外调用你的函数……你也应该研究一下使用 while 循环

while true do
 //stuff here
end

在你的最后一个 end 标签之后的最后一行添加

rubber()

即可。

2014-07-14 07:31:12
用户4378313
用户4378313

这是一个简单的修复方法,只需要在完成函数 rubber 后添加 rubber(),因为在创建函数 rubber 时,您尚未调用它启动。

2014-12-19 15:47:44
用户8076197
用户8076197

"monitor_touch" 事件是你应该使用的。另外,请确保你使用的显示器是高级显示器(带有黄色边框)。

如果需要帮助理解该事件,请查看此页面:http://computercraft.info/wiki/Monitor_touch_(event)

2017-05-28 16:36:24