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

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

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:

Which is absolutely fine. Although, when i change the window size, it appears like so...

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

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

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

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.

arampl
Party member
Posts: 240
Joined: Mon Oct 20, 2014 3:26 pm

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

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

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

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

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.

### Who is online

Users browsing this forum: RagingDave and 3 guests