Mouse inside an image
Mouse inside an image
How can i detect if the mouse is inside an image? Its for my first game ever, im really excited about it!
I can't come up with a good signature!
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: Mouse inside an image
Welcome! Good luck with your first game. Here's some code that should help you (not tested and off the top of my head):
Pass in the x/y position of the image, as well as the width and height. The function basically checks to see whether the mouse is within (or on top of) the bounding box of the image.
Code: Select all
function checkMouse(x, y, width, height)
local mx, my = love.mouse.getPosition()
return mx >= x and my >= y and mx <= x + width and my <= y + height
end
Re: Mouse inside an image
Nope. I tried to do this:
But no score is added.
Code: Select all
function love.mousepressed(x, y, button)
if button == '1' then
if checkMouse(headx, heady, head:getWidth(), head:getHeight()) == true then
score = score + 1
end
end
end
I can't come up with a good signature!
- nevon
- Commander of the Circuloids
- Posts: 938
- Joined: Thu Feb 14, 2008 8:25 pm
- Location: Stockholm, Sweden
- Contact:
Re: Mouse inside an image
That would be because there's no mouse button '1'. It's a lower-case L for left mouse button, not 1, though I do see how one could mistake them for one another.Anxiety wrote:Nope. I tried to do this:
But no score is added.Code: Select all
function love.mousepressed(x, y, button) if button == '1' then if checkMouse(headx, heady, head:getWidth(), head:getHeight()) == true then score = score + 1 end end end
Re: Mouse inside an image
Yay it works! And the lower case L is just a few pixels different from 1 in Notepad++.
I can't come up with a good signature!
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: Mouse inside an image
Mouse click, move, over, drag and drop are integrated in lQuery
Who is online
Users browsing this forum: No registered users and 230 guests