How do I make an image change when clicked?

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
Kookerus
Prole
Posts: 39
Joined: Sat Jun 28, 2014 4:33 am

How do I make an image change when clicked?

Post by Kookerus »

I want to draw an image on the screen, and make it change to a different image when the user clicks on it. How would I do that?
User avatar
Rukiri
Citizen
Posts: 68
Joined: Mon Jun 27, 2011 5:52 am

Re: How do I make an image change when clicked?

Post by Rukiri »

There is a simple command that you can use statements for such a thing and it's called "switch"

so.

Code: Select all

switch variablename {
  A: do stuff  break;
  B: do stuff break;
}
so if clicked just switch the graphic^^
User avatar
bdjnk
Citizen
Posts: 81
Joined: Wed Jul 03, 2013 11:44 pm

Re: How do I make an image change when clicked?

Post by bdjnk »

Basically you want to wait for a click (with mousereleased), check that you clicked on the original image, and replace it with your new one.

Code: Select all

function love.load(args)
	img = love.graphics.newImage("original.png")
end

function love.mousereleased(x, y, button)
	if x > 10 and x < 10 + img:getWidth() and
		y > 10 and y < 10 + img:getHeight() then
		img = love.graphics.newImage("clicked.png")
	end
end

function love.draw()
	love.graphics.draw(img, 10, 10)
end
Kookerus
Prole
Posts: 39
Joined: Sat Jun 28, 2014 4:33 am

Re: How do I make an image change when clicked?

Post by Kookerus »

bdjnk wrote:Basically you want to wait for a click (with mousereleased), check that you clicked on the original image, and replace it with your new one.

Code: Select all

function love.load(args)
	img = love.graphics.newImage("original.png")
end

function love.mousereleased(x, y, button)
	if x > 10 and x < 10 + img:getWidth() and
		y > 10 and y < 10 + img:getHeight() then
		img = love.graphics.newImage("clicked.png")
	end
end

function love.draw()
	love.graphics.draw(img, 10, 10)
end
Thank you so much! This works great!
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: How do I make an image change when clicked?

Post by bartbes »

Rukiri wrote:There is a simple command that you can use statements for such a thing and it's called "switch"
Except lua doesn't have a switch/case statement. Nor does it look like c...
Kookerus
Prole
Posts: 39
Joined: Sat Jun 28, 2014 4:33 am

Re: How do I make an image change when clicked?

Post by Kookerus »

bartbes wrote:
Rukiri wrote:There is a simple command that you can use statements for such a thing and it's called "switch"
Except lua doesn't have a switch/case statement. Nor does it look like c...
you could emulate it with an

Code: Select all

if x then do this
elseif this then do this
else do this
end
statement, but that could get quite confusing
Post Reply

Who is online

Users browsing this forum: No registered users and 191 guests