在if语句中使用computercraft变量

我有一个小问题,我是Lua和ComputerCraft的新手。所以我的问题我认为是因为我太蠢了,在if语句中编写变量更改。我会贴出我的代码希望你们可以帮我解决这个问题。谢谢

mon = peripheral.wrap("top")
modem = peripheral.wrap("right")
mon.clear()
mon.setCursorPos(1,2)
mon.blit("粉碎机","0000000000","dddddddddd")
redstone.setOutput("right", true)
Pulverizer = true
mon.setCursorPos(1,4)
mon.write("熔炉","0000000","ddddddd")
redstone.setOutput("left", true)
local Furnice = true
mon.setCursorPos(1,6)
mon.write("注液室","00000000000000000","ddddddddddddddddd")
local Injection_Chamber = true
redstone.setOutput("top",true)
mon.setCursorPos(1,8)
mon.write("冶金模拟熔炉","000000000000000000","dddddddddddddddddd")
local Metalurgig_InFuser = true
redstone.setOutput("bottom", true)
while true do
  event,side,x,y = os.pullEvent("monitor_touch")

  if x > 1 and x < 11 and y == 2 and Pulverizer == true then
    mon.setCursorPos(1,2)
    mon.clearLine()
    mon.blit("粉碎机","0000000000","eeeeeeeeee")
    redstone.setOutput("right", false)
    Pulverizer = false

 elseif x > 1 and x < 11 and y == 2 and Pulverizer == false then
   mon.setCoursorPos(1,2)
   mon.clearLine()
   mon.blit("粉碎机","0000000000","dddddddddd")
   redstone.setOutput("right", true)
   Pulverizer = true
  end
end

这是代码的Pastebin链接:[代码链接] (https://pastebin.com/06WmfbjJ)

所以问题出在代码的这一部分

if x > 1 and x < 11 and y == 2 and Pulverizer == true then
mon.setCursorPos(1,2)
mon.clearLine()
mon.blit("粉碎机","0000000000","eeeeeeeeee")
redstone.setOutput("right", false)
Pulverizer = false

粉碎机变量不想从true变成false,所以这段代码现在无法启动

   elseif x > 1 and x < 11 and y == 2 and Pulverizer == false then
   mon.setCoursorPos(1,2)
   mon.clearLine()
   mon.blit("粉碎机","0000000000","dddddddddd")
   redstone.setOutput("right", true)
   Pulverizer = true

我不知道为什么 这是一个游戏内视频链接,所以你们可以看到在视频中它变成了红色,但不再变成绿色

[https://youtu.be/R4dpN-egnwY] (https://youtu.be/R4dpN-egnwY)

点赞
用户10018042
用户10018042

不确定这是否能解决你的问题,但值得尝试调试一下。

作为良好的习惯,我建议你在第7行定义 Pulverizer 时,使用 local Pulverizer = true,以将其设置为局部变量。可能不会有区别,但这是个好习惯。根据我的经验,避免使用首字母大写的变量也是个好习惯。

其次,尝试将你的 if 块改为两个块,像这样:

if x > 1 and x < 11 and y == 2 then
  --在这里添加一些输出消息之类的东西,以便你可以看到它进入了这个代码块并正确检测到游标
    mon.setCursorPos(1,2)
    mon.clearLine()
  if Pulverizer == true then
    --在这里添加另一个输出消息,以便你可以看到 Pulverizer 被评估为 true
    mon.blit("Pulverizer","0000000000","eeeeeeeeee")
  elseif Pulverizer == false then
    --在这里添加最后一个输出消息,以便你知道如果 Pulverizer 被评估为 false
    mon.blit("Pulverizer","0000000000","dddddddddd")
  end
Pulverizer = not Pulverizer
redstone.setOutput("right", Pulverizer)
end

如果你在我标注的地方插入调试消息(灰色行),那么它应该能帮助你缩小问题的范围,因为你可以看到哪些 if 块被执行、哪些没有。另一个好的想法是在 Pulverizer = truePulverizer = false 后立即插入一些代码,以显示 Pulverizer 的值。

我对 Computercraft 的具体语法不是很熟悉,所以不确定你是否能够在聊天或仅在监视器中显示调试消息,但你明白了吧。每当遇到无法确定的问题时,尝试在代码中添加调试消息,以便你可以准确地了解发生了什么,并从那里开始解决问题。如果你获取了任何更多的信息,随时留下评论,我会查看并看看是否可以提供更多帮助。

希望你会发现这个有用,祝你有愉快的周末!

2018-07-13 20:40:36