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

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
InfamousMakundo
Prole
Posts: 3
Joined: Sun Aug 28, 2016 3:54 am

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

Post 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:
Last edited by InfamousMakundo on Sat Oct 01, 2016 12:53 am, edited 1 time in total.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

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

Post 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
Last edited by HugoBDesigner on Mon Aug 29, 2016 2:19 pm, edited 1 time in total.
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post 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
Last edited by zorg on Mon Aug 29, 2016 3:52 pm, edited 1 time in total.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

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

Post by HugoBDesigner »

Oh, you're right, for some reason I read that as right click :P

Fixed it, thanks
@HugoBDesigner - Twitter
HugoBDesigner - Blog
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

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

Post by KayleMaster »

So that's how you create instances...
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post 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.)
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
InfamousMakundo
Prole
Posts: 3
Joined: Sun Aug 28, 2016 3:54 am

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

Post 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.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

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

Post 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 ;)
@HugoBDesigner - Twitter
HugoBDesigner - Blog
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 184 guests