Page 1 of 1

label is created multiple times?

Posted: Fri Jan 08, 2016 3:01 pm
by larz

Code: Select all

function love.update(dt)
	if love.mouse.isDown(1) then
		function love.draw()
			love.graphics.setColor(0,255,0,255);
			love.graphics.printf("LMB: "..tostring(input.mouse.down.left),1,1,200,"left");
		end
	end
end
Hello, guys. I'm new in LÖVE2D, but i'm experienced with LUA.
I just want to know if this function will create a lot of labels or just 1.
I guess it's going to create labels, but anyway... i don't know how this function works in LÖVE2D.
Could someone help me? Thank you.

Re: label is created multiple times?

Posted: Fri Jan 08, 2016 3:21 pm
by Nixola
It's only going to print that variable once every frame; every frame the screen also gets cleared, so it will only make one. Every LÖVE function that prints/draws something to the screen works that way.
Welcome anyway ^^

Re: label is created multiple times?

Posted: Fri Jan 08, 2016 3:33 pm
by s-ol
it redefines love.draw every frame though, and the "if" won't work that way; the first time you press the mouse button it sets love.draw to draw the text every frame and you never change it back.

You probably want this:

Code: Select all

function love.draw()
   if love.mouse.isDown(1) then
      love.graphics.setColor(0,255,0,255);
      love.graphics.printf("LMB: "..tostring(input.mouse.down.left),1,1,200,"left");
   end
end
also there are no "labels" and nothing is "created". love.graphics.print[f] just draws something once (one frame) and doesn't create or save anything.

Re: label is created multiple times?

Posted: Fri Jan 08, 2016 3:34 pm
by zorg
Welcome, indeed!
Also, you shouldn't really define love.draw inside love.update, since it'll redefine it every frame; do it this way instead:

Code: Select all

local trigger = false
function love.update(dt)
  if love.mouse.isDown(1) then
     trigger = true
  else
     trigger = false
  end
end

function love.draw()
  if trigger then
     love.graphics.setColor(0,255,0,255);
     love.graphics.printf("LMB: "..tostring(input.mouse.down.left),1,1,200,"left");
  end
end
Or you could put the mouse button testing inside love.draw too, but that's considered bad practice, since it will combine logic with rendering.
EDIT: Ninjad :3

Re: label is created multiple times?

Posted: Fri Jan 08, 2016 3:50 pm
by larz
Alright!
Thanks for the nice reception! :awesome:
Also, how does the window width/height works?
I would like to set a label in the right-top corner of the game screen.
How could i do that?

Re: label is created multiple times?

Posted: Fri Jan 08, 2016 4:11 pm
by larz
someone?

Re: label is created multiple times?

Posted: Fri Jan 08, 2016 5:09 pm
by zorg
really simple imlpementation:

Code: Select all

local label = "I AM NOT A LABEL :3"
local width = love.window.getWidth()
function love.draw()
  love.graphics.printf(label, width, 0, width/2, 'right') -- string, x, y, limit, alignment (and other optional stuff after)
end