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

Mouse click events

Started by exploder, 29 December 2012 - 09:40 AM
exploder #1
Posted 29 December 2012 - 10:40 AM
Hello.

I have problem understanding mouse click events.

The problem is I that computer ignores Y coordinates but X coordinates works fine. That means I can click on x == 6 and y == 12 etc. and it will execute the code where it should only be executing it on the button (y == 8). The code seems to work fine when I remove all "or" from the code.

So how can I fix this problem and how can I make my code shorter without those "or" and extract them from table all at once or some other way?


Here is code that I'm working on:


while true do
time = os.time()
time = textutils.formatTime(time, true)
x,y = term.getSize()
term.setCursorPos(x/2-6,1)
print(" :-------------:")
term.setCursorPos(x/2-6,2)
print(" :Exploder's OS:")
term.setCursorPos(x/2-6,3)
print(" :-------------:")
term.setCursorPos(3,4)
print("|---------------------------------------------|")
print("  |				  Main Screen				|")
print("  |											 |")
print("  | :---------------:		:---------------:  |")
print("  | :Leave a Message:		: Read Messages :  |")
print("  | :---------------:		:---------------:  |")
print("  |											 |")
print("  | :---------------:		:---------------:  |")
print("  | :   Shutdown	:		:	  Exit	 :  |")
print("  | :---------------:		:---------------:  |")
print("  |											 |")
print("  |											 |")
print("  |											 |")
print("  |								 Time: "..time.." |")
print("  |---------------------------------------------|")
os.startTimer(0.5)
event, param1, x,y = os.pullEvent()
if event == "mouse_click" and x == 6 or x == 7 or x == 8 and y == 8 then return false -- Here is the important part.
end
end

I'm sorry for my bad English if you need better explanation then just ask.
Lyqyd #2
Posted 29 December 2012 - 11:18 AM
You would need parentheses around the x checks (since they use or, any of these that are true will make the expression true) or use range checks with and, like:

if e == "mouse_click" and x >= 6 and x <= 8 and y == 12 then
exploder #3
Posted 29 December 2012 - 11:33 AM
You would need parentheses around the x checks (since they use or, any of these that are true will make the expression true) or use range checks with and, like:

if e == "mouse_click" and x >= 6 and x <= 8 and y == 12 then

Yes, the parentheses around x fixed everything.

Thank you.