Mouse inside an image

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Mouse inside an image

Post by Anxiety »

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!
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Mouse inside an image

Post by BlackBulletIV »

Welcome! Good luck with your first game. :) Here's some code that should help you (not tested and off the top of my head):

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
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.
User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Re: Mouse inside an image

Post by Anxiety »

Nope. I tried to do this:

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
But no score is added.
I can't come up with a good signature!
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Mouse inside an image

Post by nevon »

Anxiety wrote:Nope. I tried to do this:

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
But no score is added.
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.
User avatar
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

Re: Mouse inside an image

Post by Anxiety »

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!
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Mouse inside an image

Post by BlackBulletIV »

Good stuff :)
User avatar
RPG
Party member
Posts: 157
Joined: Wed Mar 02, 2011 5:02 am
Location: Russia
Contact:

Re: Mouse inside an image

Post by RPG »

Mouse click, move, over, drag and drop are integrated in lQuery :cool:
Post Reply

Who is online

Users browsing this forum: No registered users and 230 guests