[Solved]Re-drawing an image upon setColor?

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
Relazy
Citizen
Posts: 51
Joined: Thu Jul 10, 2014 11:10 pm

[Solved]Re-drawing an image upon setColor?

Post by Relazy »

Planned outcome:
rectangle and text:'mouseover is false' becomes blue if mouseover is true and red is mousover is false
actual outcome:
rectangle and text is drawn blue underneath if mouseover is true and the red copies of text and rectangle stay on top if done repeatedly this causes an out of memory error
problem:
new image is drawn every time the mouseover is change from false to true or true to false
Comments
Edit: maths within button_update() is simplified for troubleshooting and does not benefit the outcome

How do I solve this? and get the planned outcome? or is there a better method? sorry but I am quite new to love2d therefore I don't know any better.

heres the code:
main.lua,

Code: Select all

require "gui"

function love.load()
    background = love.graphics.newImage("background.jpg")
    love.graphics.setNewFont(12)
     -- camera variable is created with sub variables such as .scale and .rotation
end

function love.draw()
    background = love.graphics.newImage("background.jpg")
    love.graphics.draw(background)
	button_create()
	button_draw()
	love.graphics.print("mouse position x is:"..mousex)
	love.graphics.print("mouse position y is:"..mousey,10,10 )
	
end

function love.update(dt)
	button_update(dt)
end
gui.lua

Code: Select all

button = {}
function button_create(x,y,j,b,n,m)
	-- usage: button_create(x,y,text,id,width of rectangle, length of rectangle)
	table.insert(button,{x = x or love.graphics.getWidth()/2, y = y or love.graphics.getHeight()/2, j = j or "none", b = b or "none", n = n or 60, m = m or 20, mouseover = false})
end

function button_draw()
	for i,v in ipairs(button) do
		love.graphics.setColor(255,255,255)
		if v.mouseover == false then
			love.graphics.setColor(255,0,0)
		end
		if v.mouseover == true then
			love.graphics.setColor(0,0,255)
		end
		love.graphics.print("mouseover is:"..(tostring(v.mouseover)),30, 30)
		love.graphics.rectangle("fill",v.x,v.y,v.n,v.m)
		love.graphics.setColor(0,0,0)
		love.graphics.print(v.j,v.x,v.y)
		love.graphics.setColor(255,255,255)
		end
		
end

function button_update()
	mousey = love.mouse.getY()
	mousex = love.mouse.getX()
	for i,v in ipairs(button) do
		if mousex > 10 and
		mousey > 10 then
			v.mouseover = true
		else
			v.mouseover = false
		end
	end
	return mousex,mousey
end

Attachments
this.love
love 0.9.1
(38.35 KiB) Downloaded 56 times
Last edited by Relazy on Fri Jul 11, 2014 1:31 am, edited 1 time in total.
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: Re-drawing an image upon setColor?

Post by veethree »

You're calling "button_create()" inside love.draw. love.draw is called every frame, So what's happening is that you're creating a new button every frame.

What you should do is create the button in love.load().

Your button_update function won't work properly either cause you're not using the buttons position. and it doesn't actually check if the mouse is inside the button.

I wrote a little example you could look at if you want, I've set it up in a similar way as you did.
Relazy
Citizen
Posts: 51
Joined: Thu Jul 10, 2014 11:10 pm

Re: Re-drawing an image upon setColor?

Post by Relazy »

veethree wrote:You're calling "button_create()" inside love.draw. love.draw is called every frame, So what's happening is that you're creating a new button every frame.

What you should do is create the button in love.load().

Your button_update function won't work properly either cause you're not using the buttons position. and it doesn't actually check if the mouse is inside the button.

I wrote a little example you could look at if you want, I've set it up in a similar way as you did.
I was aware that the button_update won't check if the mouse was inside the button. I taught that the problem was within my maths so I swapped for a simpler equation.sorry, I didn't mention that in the original post. Thanks for the quick reply. I have changed the code and everything works as it should. I appreciate the help. :ultraglee:
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests