Is there a simple way/library to detect hovering, keypress over an object(such as an image, or a shape) ?
All methods i see online and in forums, refer to the idea where we trace back to see if the mouse-co ordinates'range is with the image position co-ordinates. But i guess, there should be a simple logical implementation, shouldnt it ?
Like place n number of images on screen, and when mouse hovers over them detect it, even if need be even the dynamically position updated images too can be detected. Or am i missing something. ?
The simplest code for image that i could think of and generate is
Code: Select all
local image1, image2 -- Define variables to hold the images
function love.load()
-- Load images
image1 = love.graphics.newImage("tbc.png")
image2 = love.graphics.newImage("sas.jpg")
-- Set the positions where you want to display the images
image1X, image1Y = 100, 100
image2X, image2Y = 300, 100
end
function love.draw()
-- Draw images at specified positions
love.graphics.draw(image1, image1X, image1Y)
love.graphics.draw(image2, image2X, image2Y)
end
function love.update(dt)
-- Get the mouse coordinates
local mouseX, mouseY = love.mouse.getPosition()
-- Check if mouse is hovering over image1
if mouseX >= image1X and mouseX <= image1X + image1:getWidth() and
mouseY >= image1Y and mouseY <= image1Y + image1:getHeight() then
-- Mouse is hovering over image1
print("Mouse is over image1")
end
-- Check if mouse is hovering over image2
if mouseX >= image2X and mouseX <= image2X + image2:getWidth() and
mouseY >= image2Y and mouseY <= image2Y + image2:getHeight() then
-- Mouse is hovering over image2
print("Mouse is over image2")
end
end