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

Cannot get the screen to touch correctly?

Started by SNWLeader, 05 February 2013 - 02:52 PM
SNWLeader #1
Posted 05 February 2013 - 03:52 PM
This is realy getting annoying.
I want the screen to touch out of two places to select a choice when verifying the transaction and the screen will not complete the action on either.

This code is still in developement so no saying I am ripping people off.

here is my code.

local function printGUI()
  monitor.setCursorPos(1,1)
  monitor.write("_____________  _____________  _____________  ______  _______ ")
  monitor.setCursorPos(1,2)
  monitor.write(":Add Chicken:  :Add Beef   :  :Add Pork   :  :DONE:  :CLEAR: ")
  monitor.setCursorPos(1,3)
  monitor.write("_____________  _____________  _____________  ______  ______")
  monitor.setCursorPos(1,4)
  monitor.write("______________________________ ")
  monitor.setCursorPos(1,5)
  monitor.write("Chicken: "..cNum)
  monitor.setCursorPos(1,6)
  monitor.write("Beef: "..bNum)
  monitor.setCursorPos(1,7)
  monitor.write("Pork: "..pNum)
  monitor.setCursorPos(1,8)
  monitor.write("Total Price: "..tdue.."$")
  monitor.setCursorPos(1,9)
  monitor.write("Press DONE when finished.")
end
local function getBal()
  handle = fs.open("disk/creditCard", "r")
  aNum = tonumber(handle.readAll())
  bal = math.sqrt(aNum)
return bal
end
local function subBal(tdue)
getBal()
handle = fs.open("disk/creditCard", "w")
handle.write("				    ")
newbal = (bal - tdue) * (bal - tdue)
handle.write(newbal)
handle.close()
end
local function clearScreen()
  monitor.clear()
  printGUI()
end
local function printTitle()
monitor.clear()
monitor.setCursorPos(1,5)
monitor.write("Press Screen to continue....")
sleep(1)
end
monitor = peripheral.wrap("right")
mside = "right"
screen = 0   
cNum = 0
bNum = 0
pNum = 0
tdue = 0
while true do
printTitle()
local event, p1 = os.pullEvent()
if event == "monitor_touch" then
screen = 1
repeat
   clearScreen() 
   local event, side, x, y = os.pullEvent("monitor_touch")
   if x >= 1 and x <= 13 then
	 if y == 1 or y == 2 or y == 3 then
	   cNum = cNum + 1
	   tdue = tdue + 5
	 end
   elseif x >= 16 and x <= 29 then
	 if y == 1 or y == 2 or y == 3 then
	   bNum = bNum + 1
	   tdue = tdue + 5
	 end
   elseif x >= 31 and x <= 44 then
	 if y == 1 or y == 2 or y == 3 then
	 pNum = pNum + 1
	 tdue = tdue + 5
	 end
   elseif x >= 46 and x <= 51  then
	 if y == 1 or y == 2 or y == 3 then
	 monitor.setCursorPos(1,10)
	 monitor.write("Please enter card now. Touch screen to continue")
	 event, side = os.pullEvent()
	  if event == "monitor_touch" then
	  monitor.clear()
	  monitor.setCursorPos(1,5)
	  getBal()
	  if bal >= tdue then
	  monitor.write("Your balance is "..bal)
	  monitor.setCursorPos(1,6)
	  monitor.write("The amount due is "..tdue)
	  monitor.setCursorPos(1,7)
	  monitor.write("Do you wish to pay it?")
	  monitor.setCursorPos(1,1)
	  monitor.write(" _____	  ____")
	  monitor.setCursorPos(1,2)
	  monitor.write(" : YES	  NO")
	  local event, side, x, y = os.pullEvent("monitor.touch")    -- I cannot get the darn thing to procceed with the selection here.
	   if x >= 3 and x <= 5 then
	   if y == 1 or y == 2 or y == 3 then
	   monitor.setCursorPos(1,8)
	   monitor.write("Completing Transaction")
	   subBal(tdue)
	   end
	   elseif x == 12 or x == 13 then
	   if y == 1 or y ==2 or y == 3 then
	   monitor.setCursorPos(1,8)
	   monitor.write("Canceling Transaction")
	   end
	  end
	 sleep(1)
	 screen = 0
	 pNum = 0
	 bNum = 0
	 cNum = 0
	 tdue = 0
	 end
   elseif x >= 53 and x <= 60 then
	 if y == 1 or y == 2 or y == 3 then
	 cNum = 0
	 bNum = 0
	 pNum = 0
	 tdue = 0
	 end
   end
end  
end
until screen == 0
  end
end
theoriginalbit #2
Posted 05 February 2013 - 03:54 PM
wrong event
"monitor.touch"
its
"monitor_touch"
Orwell #3
Posted 05 February 2013 - 03:56 PM
The correct event name is monitor_touch instead of monitor.touch. :)/> You can find more on the wiki: http://computercraft...uch_%28event%29

Edit: ninja'd >.<
SNWLeader #4
Posted 05 February 2013 - 04:01 PM
I derped on the local event, side = os.pullEvent("monitor_touch") part is a dot, but now the transaction part is driving me nuts….
I need to clear on the card what the total is and write the new total….and I do not know how.

The correct event name is monitor_touch instead of monitor.touch. :)/> You can find more on the wiki: http://computercraft...uch_%28event%29

Edit: ninja'd >.<
and yes you got ninja'd as you ninja'd me.
Kingdaro #5
Posted 05 February 2013 - 04:09 PM
On your subBal function, when you open the file with writing mode, all content is automatically erased, so writing a bunch of white space isn't needed. In order to write your new balance, you need to convert it to a string first, so just do that.


local function subBal(tdue)
  getBal()
  handle = fs.open("disk/creditCard", "w")
  newbal = (bal - tdue) * (bal - tdue)
  handle.write(tostring(newbal))
  handle.close()
end
SNWLeader #6
Posted 05 February 2013 - 04:36 PM
That fixed the problem.