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

Mouse click event, changes 2 settings instead of 1.

Started by Dustmuz, 27 November 2015 - 03:38 PM
Dustmuz #1
Posted 27 November 2015 - 04:38 PM
First off: The Code

the code does NOT give off an error, it works as it is now, the problem is the coding in some way

This program is written for a advanced pocket computer (wireless)
the problem lies at the mouse click event..

i have 3 locations im watching (so to speak)
X10 or 11 and Y2
X7 or 8 and Y3
X10 or 11 and Y8

whenever i click
X10 or X11 and Y2
OR
X10 or X11 and Y8

it triggers both of them, and not just one of them.. tried changing a few things.. but that didnt work out for me
would i be better off splitting them into 2 seperate funtions?? with 2 os.pullEvents
Creator #2
Posted 27 November 2015 - 05:00 PM
Don't use multiple ifs. Use if elseif. It checks if the first condition is true. If it is, it executes the code and goes to the end. Else, it goes tot eh next elseif and checks if it is true.
valithor #3
Posted 27 November 2015 - 06:17 PM
The problem is how the program is actually reading it.

The computer reads:
X==10 or X==11 and Y==2

As if X == 10, or if X == 11 and Y == 2. So if X == 10 then it will be true and the if statement will run, without even checking the Y value.

The easiest fix would just to put parenthesis around the or statement like:
(X==10 or X==11) and Y==2

Although that would work, most people do buttons a little different:

if MouseX >=10 and MouseX<=11 and MouseY==2 then
  --# it was pressed
end

This just lets you make buttons that are bigger without having to manually put in every value (you would just have to increase the range).

edit:

Cleaned it up a little to make it easier to read.
Edited on 27 November 2015 - 09:53 PM
Dustmuz #4
Posted 28 November 2015 - 09:36 AM
Creator and Valithor..

Thanks for your inputs..
it worked out for me using the >= <=
will be rewriting the ifs to elseifs (luckily thats easy) :D/>