Page 1 of 1

How to make a clicked image (button) draw an image[SOLVED]

Posted: Sun Aug 28, 2016 4:03 am
by InfamousMakundo
I made a button with is currenctly a .png file and i want it to make another image appear when it is left clicked by the mouse, please help thanks. :cry:

Re: How to make a clicked image (button) draw an image

Posted: Mon Aug 29, 2016 2:10 pm
by HugoBDesigner
If I understand you correctly, you have a button image, and once you right-click it an image shows up on screen, like a pop-up, right? If so:

Code: Select all

function love.load()
	--This is one way of making a button. You can make it however you want, even use pre-made libraries
	myButton = {
		x = 10, y = 10, image = love.graphics.newImage("myButton.png"), clicked = false
	}
	
	--This is just an easy way to know the pop-up image and where to draw it. Change to whatever you want
	myImage = {
		x = 50, y = 50, image = love.graphics.newImage("myImage.png")
	}
end

function love.draw()
	love.graphics.draw(myButton.image, myButton.x, myButton.y) --This is where we draw your button
	
	if myButton.clicked then --If the person clicked the button, this will be true
		love.graphics.draw(myImage.image, myImage.x, myImage.y) --This is where we draw the pop-up image
	end
end

function love.mousepressed(x, y, button)
	if button == 1 then --Left click
		if x >= button.x and x <= button.x+button.image:getWidth() and y >= button.y and y <= button.y+button.image:getHeight() then --Detect if the click was inside the button
			button.clicked = true --This is what triggers the pop-up image
		end
	end
end

Re: How to make a clicked image (button) draw an image

Posted: Mon Aug 29, 2016 2:12 pm
by zorg
InfamousMakundo wrote:when it is left clicked by the mouse
Change the 2 in the above code to 1, in the love.mousepressed function.
Edit: has been fixed. :3

Re: How to make a clicked image (button) draw an image

Posted: Mon Aug 29, 2016 2:19 pm
by HugoBDesigner
Oh, you're right, for some reason I read that as right click :P

Fixed it, thanks

Re: How to make a clicked image (button) draw an image

Posted: Tue Aug 30, 2016 10:53 am
by KayleMaster
So that's how you create instances...

Re: How to make a clicked image (button) draw an image

Posted: Tue Aug 30, 2016 11:08 am
by zorg
KayleMaster wrote:So that's how you create instances...
No, the example code was just simple encapsulation. I can't see anything related to instances in it.
Also, please don't derail threads. If you want to ask stuff, go to the irc. :3 (whereby you'll be probably told to read the PIL anyway.)

Re: How to make a clicked image (button) draw an image

Posted: Mon Sep 05, 2016 8:11 pm
by InfamousMakundo
HugoBDesigner wrote: if myButton.clicked then
I changed this to

Code: Select all

if click then
also changed this
HugoBDesigner wrote:button.clicked = true
to

Code: Select all

 click = true
then the code started working better for me, thanks for your help Hugo.

Re: How to make a clicked image (button) draw an image

Posted: Mon Sep 05, 2016 9:36 pm
by HugoBDesigner
Glad I could help :3

I suggest you add "[SOLVED]" to your title, so other people with similar questions can find this more easily ;)