This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
DRoleplaysBG's profile picture

Program closes even when not all if's are done

Started by DRoleplaysBG, 15 December 2017 - 07:33 PM
DRoleplaysBG #1
Posted 15 December 2017 - 08:33 PM
So I am building an Pocket PC OS and I've ran into this problem:
I have 2 touchscreen if's (starting at line 47) and they are at the end. When I click somewhere for the first time, it's fine and if it's on the correct x and y - it runs the code that should run. However, if I do that one time the program stops (without any error) and puts up the craftos shell. I am expecting an second click to be made and then the program to shut down.
Code:

term.setBackgroundColor(colors.lime)
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.white)
print("jOS Loading...")
-- BEGGINING: LOADING
local signal = 0
local version = 1
-- ENDING: LOADING
sleep(3)
function home()
  term.setCursorPos(1,1)
  term.setBackgroundColor(colors.lightBlue)
  term.clear()
  term.setCursorPos(1,1)
  term.setBackgroundColor(colors.white)
  term.clearLine()
  term.setTextColor(colors.black)
  print("jOS "..version)
  term.setCursorPos(18,1)
  if signal == 1 then
  term.write("Connected")
  else
  term.write("No signal")
  end
  term.setCursorPos(1,20)
  term.setBackgroundColor(colors.black)
  term.clearLine()
  term.setTextColor(colors.white)
  write("YouAd   SMS")
end
function Ads()
  term.setCursorPos(1,2)
  term.setTextColor(colors.white)
  term.setBackgroundColor(colors.yellow)
  term.clearLine()
  write("hello")
end
function SMS()
  home()
end
home ()
[b]-- Touchscreen
local event, button, xPos, yPos = os.pullEvent("mouse_click")
if (xPos > 0 and xPos < 5) and (yPos > 19 and yPos < 21) then
  home()
  Ads()
end[/b]
if (xPos > 8 and xPos < 11) then
  home()
  SMS()
end

Thanks in advance!
KingofGamesYami #2
Posted 16 December 2017 - 02:40 AM
You pull 1 event. The program will end after 1 event. I suggest adding a loop to pull and compare an infinate number of events.
Bomb Bloke #3
Posted 16 December 2017 - 09:03 AM
See the sections here regarding "while", "for", and "repeat" to see how loops (repeating blocks of code) are constructed.
Edited on 16 December 2017 - 08:03 AM