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

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
User avatar
Olee
Prole
Posts: 3
Joined: Mon Jul 27, 2015 12:56 pm

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

Post by Olee » Mon Jul 27, 2015 2:11 pm

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

User avatar
Ulydev
Party member
Posts: 428
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

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

Post by Ulydev » Mon Jul 27, 2015 2:35 pm

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.

User avatar
arampl
Party member
Posts: 239
Joined: Mon Oct 20, 2014 3:26 pm

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

Post by arampl » Mon Jul 27, 2015 2:40 pm

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

User avatar
DaedalusYoung
Party member
Posts: 402
Joined: Sun Jul 14, 2013 8:04 pm

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

Post by DaedalusYoung » Tue Jul 28, 2015 3:25 am

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.

Post Reply

Who is online

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