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
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