Page 1 of 1

Logic error: love.draw is drawing everywhere!!!

Posted: Mon Jul 27, 2015 2:11 pm
by Olee
Hello all, so basically my problem is that I've created a table called button, which creates buttons for main menu's, settings, etc and when i re-size the window, new text is created which fits the window size. Whereas i just wanted the old text to just update.

Code: Select all

button = {}

function button.spawn(x, y, text)
	table.insert(button, {x = x, y = y, text = text})

end

function button.draw()
	for i, v in ipairs(button) do
		love.graphics.setColor(255,255,255)
		love.graphics.print(v.text, v.x, v.y)
	end
end

Code: Select all

if gamestate == "paused" then
		button.draw()
		love.graphics.getFont("Android.ttf")
		local width = font:getWidth("Start") --gets the width of the argument in pixels for this font
		button.spawn(windowWidth/2-width/2, windowHeight/4, "Start")
		local width = font:getWidth("Settings") --gets the width of the argument in pixels for this font
		button.spawn(windowWidth/2-width/2, windowHeight/2, "Settings")
		local width = font:getWidth("Quit") --gets the width of the argument in pixels for this font
		button.spawn(windowWidth/2-width/2, windowHeight/4+windowHeight/2, "Quit")
		print(button)
	end
When i use the function button.spawn it spawns fine in the correct size, font and coordnates. But when i resize the window the font should stay central to the screen.

Loading up the game prints like so:
Image
Which is absolutely fine. Although, when i change the window size, it appears like so...
Image
I don't understand why the old text stays, but doesn't update.

And by the way, yes, I do understand that i will need to update the 'windowHeight' and font size variable to come in scale with the window but I just haven't got around to doing it yet.

Thanks a lot,

Olee

Re: Logic error: love.draw is drawing everywhere!!!

Posted: Mon Jul 27, 2015 2:35 pm
by Ulydev
I don't understand, could you provide a .love ? Are you calling button.spawn when the window is resized ? If so, that's what might be causing the problem.

Re: Logic error: love.draw is drawing everywhere!!!

Posted: Mon Jul 27, 2015 2:40 pm
by arampl
And what do you mean by:

Code: Select all

love.graphics.getFont("Android.ttf")
?

This function should be used this way:

Code: Select all

font = love.graphics.getFont()

Re: Logic error: love.draw is drawing everywhere!!!

Posted: Tue Jul 28, 2015 3:25 am
by DaedalusYoung
From what I can tell with this code, it looks like you keep creating new buttons all the time. Even if the window size doesn't change, you keep adding the same buttons over and over. Then when the window resizes, all these hundreds of buttons drawn on top of each other are still there and the game just creates a whole new load of buttons on the new location.

Don't do this.

Spawn in your buttons once, for example, in a load function. Then, when the window size changes (LÖVE has a callback for this), you clear the buttons table, and refill it again with buttons on the new location.

Or use love.scale in your love.draw function, so you don't have to worry about anything and your buttons will appear where you want them and always at the same size, relative to the window, but that's a bit more advanced.