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

(mouse click) #Detects when the x is between 1-5 and y is equal to 1

Started by LabyStudio, 15 July 2013 - 08:20 AM
LabyStudio #1
Posted 15 July 2013 - 10:20 AM

local event, btn, mousex, mouseY = os.pullEvent("mouse_click")
If mouseX>=1 and mouseX=<5 and mouseY==1 then --#Detects when the x is between 1-5 and y is equal to 1
  print("test")
end
Bubba #2
Posted 15 July 2013 - 11:09 AM
You should use tables if you need to detect when random coordinates are clicked:


local coords = {
  1,2,1,3,1,4,1,5,1,6
}

local event, btn, mouseX, mouseY = os.pullEvent("mouse_click")
for index=1, #coords, 2 do --#This will increment by two
  if mouseX == coords[index] and mouseY==coords[index+1] then
    print("test")
  end
end

If you need coordinates that are in a range, use a simple if statement:

local event, btn, mousex, mouseY = os.pullEvent("mouse_click")
if mouseX>=1 and mouseX=<5 and mouseY==1 then --#Detects when the x is between 1-5 and y is equal to 1
  print("test")
end
LabyStudio #3
Posted 15 July 2013 - 11:20 AM
thank you
LabyStudio #4
Posted 15 July 2013 - 11:48 AM

local event, btn, mousex, mouseY = os.pullEvent("mouse_click")
if mouseX>=1 and mouseX=<5 and mouseY==1 then --#Detects when the x is between 1-5 and y is equal to 1
  print("test")
end

'then' expected! :/
lieudusty #5
Posted 15 July 2013 - 11:51 AM
I think your comparison signs are mixed up. Try switching them around: >= to <=
LabyStudio #6
Posted 15 July 2013 - 11:56 AM
I think your comparison signs are mixed up. Try switching them around: >= to <=


local event, btn, mousex, mouseY = os.pullEvent("mouse_click")
if mouseX<=1 and mouseX<=5 and mouseY==1 then                         -- [ <= ]
  print("test")
end

attempt to compare __le on nil an number :/

and:


local event, btn, mousex, mouseY = os.pullEvent("mouse_click")
if mouseX<=1 and mouseX=<5 and mouseY==1 then                        -- [ =< ]
  print("test")
end

'then' expected! :/
Kingdaro #7
Posted 15 July 2013 - 12:05 PM
Please look thoroughly in your code before pasting it here again.


local event, btn, mouseX, mouseY = os.pullEvent("mouse_click") --# the first "mouseX" here wasn't capitalized.
if mouseX >= 1 and mouseX <= 5 and mouseY == 1 then              --# also, the first symbol should be >=
  print("test")
end
Zudo #8
Posted 15 July 2013 - 12:10 PM
And please use the
 tags
Lyqyd #9
Posted 15 July 2013 - 01:12 PM
Do not remove your title or question post. Please put them back so that your question will be findable in the search again.