Jump to content




Checking If A Point On An Image Is Clicked


  • You cannot reply to this topic
6 replies to this topic

#1 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 20 September 2013 - 10:44 AM

Hello com,
I'm working on a game and I want to check if the user has clicked a specific position on the screen. The position(s) is/are an image drawn with the paint program.
I want to check if the click was on the image.

I'm doing something like this but it don't works:
local img = paintutils.loadImage("someImage")
local clickCount = 1
local function draw()
while true do
  paintutils.drawImage(img)
  sleep(0.1)
end
end
local function click()
while true do
  local _,btn,x,y = os.pullEvent()
  if _ == "mouse_click" then
   if img[y][x] == colors.brown then
    clickCount = clickCount + 1
   end
  end
  sleep(0)
end
end
Does anybody know what I'm doing wrong?

#2 BigTwisty

  • Members
  • 106 posts

Posted 20 September 2013 - 01:29 PM

The problem is with this line:
local function click()

This sets up the rest of the code as a function which is then never called by your program. Easiest solution is to just add "click()" to the bottom of the code. Better would be to remove "local function click()" and the last "end".

Edit: Better use of indentation might help you a lot. For example:
local img = paintutils.loadImage("someImage")
local clickCount = 1
local function draw()
  while true do
    paintutils.drawImage(img)
    sleep(0.1)
  end
end
while true do
  local id,btn,x,y = os.pullEvent()
  if id == "mouse_click" then
    if img[y][x] == colors.brown then
      clickCount = clickCount + 1
    end
  end
  sleep(0)
end

Do you see how the extra tabbing helps you to see where the functions start and end?

#3 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 20 September 2013 - 02:03 PM

I think, you are missing parallel API usage in your program:

parallel.waitForAll(draw, click)

EDIT:
First goes y, then x coordinates.

Next, I think that this:

if img[y][x] == colors.brown then

should be that:

if img[x][y] == colors.brown then

--//First y then x.

Edited by MKlegoman357, 20 September 2013 - 02:09 PM.


#4 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 20 September 2013 - 03:46 PM

View PostMKlegoman357, on 20 September 2013 - 02:03 PM, said:

I think, you are missing parallel API usage in your program:

parallel.waitForAll(draw, click)
-snip-
Yep, I forgot the parallel call in my code above :D

#5 BigTwisty

  • Members
  • 106 posts

Posted 20 September 2013 - 11:58 PM

Why draw the image over and over again?

#6 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 21 September 2013 - 02:10 AM

View PostBigTwisty, on 20 September 2013 - 11:58 PM, said:

Why draw the image over and over again?
Because I want to add a menu and then it has to be redrawn every time :D

#7 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 21 September 2013 - 02:29 AM

Why not draw the image in your event loop, just before pulling an event? And remove the sleep(0) from your event handling loop.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users