How do I make an image change when clicked?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
How do I make an image change when clicked?
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?
Re: How do I make an image change when clicked?
There is a simple command that you can use statements for such a thing and it's called "switch"
so.
so if clicked just switch the graphic^^
so.
Code: Select all
switch variablename {
A: do stuff break;
B: do stuff break;
}
Re: How do I make an image change when clicked?
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
Re: How do I make an image change when clicked?
Thank you so much! This works great!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
- 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?
Except lua doesn't have a switch/case statement. Nor does it look like c...Rukiri wrote:There is a simple command that you can use statements for such a thing and it's called "switch"
Re: How do I make an image change when clicked?
you could emulate it with anbartbes wrote:Except lua doesn't have a switch/case statement. Nor does it look like c...Rukiri wrote:There is a simple command that you can use statements for such a thing and it's called "switch"
Code: Select all
if x then do this
elseif this then do this
else do this
end
Who is online
Users browsing this forum: No registered users and 191 guests