How to detect a click on an image

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Mathamagic
Prole
Posts: 3
Joined: Sat Apr 16, 2022 5:54 pm

How to detect a click on an image

Post by Mathamagic »

i am currently coding a game and i want to create a button with an image and i want it to draw something whenever it is pressed and ive looked at the wiki but i have no idea how to do it
User avatar
BrotSagtMist
Party member
Posts: 662
Joined: Fri Aug 06, 2021 10:30 pm

Re: How to detect a click on an image

Post by BrotSagtMist »

Unless you are tilting the image or give it cutouts this is just a simple range check:

Code: Select all

if love.mouse.isDown( button ) and MX>x and MX<x+width and MY>y and MY<y+height then
Where MX/MY is the mouseposition you need to grab prior, x/y the picture position and you need to grab its size too.
obey
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: How to detect a click on an image

Post by ReFreezed »

You can use love.mousepressed to detect mouse clicks, together with mouse position range checking like BrotSagtMist said, for example like this:

Code: Select all

local image = love.graphics.newImage("coolButton.png")

local button = {
	image  = image,
	x      = 150,
	y      = 100,
	width  = image:getWidth(),
	height = image:getHeight(),
}

function love.mousepressed(mx, my, mbutton)
	if mbutton == 1 and mx >= button.x and mx < button.x+button.width and my >= button.y and my < button.y+button.height then
		print("Pressed button!")
	end
end

function love.draw()
	love.graphics.draw(button.image, button.x, button.y)
end
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
Mathamagic
Prole
Posts: 3
Joined: Sat Apr 16, 2022 5:54 pm

Re: How to detect a click on an image

Post by Mathamagic »

BrotSagtMist wrote: Sat Apr 16, 2022 8:06 pm Unless you are tilting the image or give it cutouts this is just a simple range check:

Code: Select all

if love.mouse.isDown( button ) and MX>x and MX<x+width and MY>y and MY<y+height then
Where MX/MY is the mouseposition you need to grab prior, x/y the picture position and you need to grab its size too.
whenever i try to set the x/y and MX/MY and width and height it says "attempt to compare number with function" how do i fix this?
MrFariator
Party member
Posts: 563
Joined: Wed Oct 05, 2016 11:53 am

Re: How to detect a click on an image

Post by MrFariator »

As the error states, you're trying to compare a number value with a function. If you're attempting to use love.mouse.getX, or similar functions, along the lines of:

Code: Select all

local x = 100
local mx = love.mouse.getX -- a function value
if x > mx then -- error, mx is a function value
  -- ...rest of code
end
Then you'll get an error. You'll have to remember to call the function to get the actual value, like so:

Code: Select all

local x = 100
local mx = love.mouse.getX() -- added the parentheses, now mx has a number value returned by the function
if x > mx then -- no error
  -- ...rest of code
end
When you get errors and don't understand the error, it's better to post your code so we can give more pointed advice on what's happening.
Mathamagic
Prole
Posts: 3
Joined: Sat Apr 16, 2022 5:54 pm

Re: How to detect a click on an image

Post by Mathamagic »

ReFreezed wrote: Sat Apr 16, 2022 9:05 pm You can use love.mousepressed to detect mouse clicks, together with mouse position range checking like BrotSagtMist said, for example like this:

Code: Select all

local image = love.graphics.newImage("coolButton.png")

local button = {
	image  = image,
	x      = 150,
	y      = 100,
	width  = image:getWidth(),
	height = image:getHeight(),
}

function love.mousepressed(mx, my, mbutton)
	if mbutton == 1 and mx >= button.x and mx < button.x+button.width and my >= button.y and my < button.y+button.height then
		print("Pressed button!")
	end
end

function love.draw()
	love.graphics.draw(button.image, button.x, button.y)
end
thank you so much this perfectly :ultraglee:
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: How to detect a click on an image

Post by milon »

I suggest you use love.mousereleased instead of love.mousepressed. You typically want the click action to occur when the player releases the button, especially if timing is important. Personally, I take it one step further. I use love.mousepressed() to register which element was clicked on - if any - and compare to which element the mouse was over when love.mousereleased() was called. If they're not the same element, I just return and don't do anything else (the user moused away from the gui element before releasing the click).
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 7 guests