Page 1 of 2

How to undraw something?

Posted: Mon Jan 30, 2012 11:12 pm
by legendman3
Like when i call to draw a image:

Code: Select all

love.graphics.draw(trollface, trollx, trolly)
How can i undraw it?

Re: How to undraw something?

Posted: Mon Jan 30, 2012 11:18 pm
by tentus
Anything you draw will be erased before the next frame renders. Hence, to "undraw" something, simply don't draw it again the next frame/

Re: How to undraw something?

Posted: Mon Jan 30, 2012 11:20 pm
by legendman3
Um... Hehe, how would i stop drawing it?

Re: How to undraw something?

Posted: Mon Jan 30, 2012 11:20 pm
by tentus
Here, we just had a thread on this:
viewtopic.php?f=3&t=7664

Re: How to undraw something?

Posted: Mon Jan 30, 2012 11:24 pm
by veethree
Use a boolean to tell löve when you want it to be drawn. Here's an example

Code: Select all

function love.load()
	drawtroll = true
end

function love.draw()
	if drawtroll == true then
		love.graphics.draw(trollface, trollx, trolly)
	end
end

function love.keypressed(key)
	if key == "q" then
		drawtroll = true
	elseif key == "w" then
		drawtroll = false
	end
end
If you put that into main.lua and run it when you press q it will draw the image, and when you press w it wont.

Re: How to undraw something?

Posted: Mon Jan 30, 2012 11:27 pm
by legendman3
Tentus, ha guess i didnt search well enough.

Vee, Thanks.

Re: How to undraw something?

Posted: Tue Jan 31, 2012 12:21 am
by tentus
veethree wrote:

Code: Select all

function love.draw()
	if drawtroll == true then
		love.graphics.draw(trollface, trollx, trolly)
	end
end
Just so you know, there's an even more convenient way to write that in Lua:

Code: Select all

function love.draw()
	if drawtroll then
		love.graphics.draw(trollface, trollx, trolly)
	end
end
In Lua, you don't have to say == true, and if you want to see if something is false, you can just say not before the variable name. I find that these two little details contribute greatly towards the legibility of Lua.

Re: How to undraw something?

Posted: Tue Jan 31, 2012 12:37 am
by veethree
tentus wrote:
veethree wrote:

Code: Select all

function love.draw()
	if drawtroll == true then
		love.graphics.draw(trollface, trollx, trolly)
	end
end
Just so you know, there's an even more convenient way to write that in Lua:

Code: Select all

function love.draw()
	if drawtroll then
		love.graphics.draw(trollface, trollx, trolly)
	end
end
In Lua, you don't have to say == true, and if you want to see if something is false, you can just say not before the variable name. I find that these two little details contribute greatly towards the legibility of Lua.
That is indeed convenient as hell..wish i had found out about that sooner. haha

Re: How to undraw something?

Posted: Tue Jan 31, 2012 7:56 am
by T-Bone
tentus wrote:
veethree wrote:

Code: Select all

function love.draw()
	if drawtroll == true then
		love.graphics.draw(trollface, trollx, trolly)
	end
end
Just so you know, there's an even more convenient way to write that in Lua:

Code: Select all

function love.draw()
	if drawtroll then
		love.graphics.draw(trollface, trollx, trolly)
	end
end
In Lua, you don't have to say == true, and if you want to see if something is false, you can just say not before the variable name. I find that these two little details contribute greatly towards the legibility of Lua.
That's not specific to Lua. As a matter of fact, I can't think of a single programming language where you can't as long as "drawtroll" is a boolean (although the specific syntax of course varies).

Re: How to undraw something?

Posted: Tue Jan 31, 2012 3:26 pm
by tentus
In lua you're not constrained to boolean though.

Code: Select all

trol = {}
if trol then
   print("yes")
end
The above will print "yes". trol could have also been a number, a string, or even a function. The only times it wouldn't print "yes" is when trol = false or trol = nil (the equivalent of trol never being defined in the first place).

An example use of this: in my RPG, some objects react when you collide with them (I'm using HardonCollider btw). Others are solid. Some are both, and some are neither. Here's the ENTIRE collision function:

Code: Select all

function on_collide(dt, a, b, x,y)
	if ents[b.id].reaction then
		ents[b.id]:reaction()
	end
	if b.solid then
		a:move(x,y)
	end
end
This works because a) the player is the only active shape, meaning that the parameter a is always the player, and b) every shape has an id defined that lets me know where the object is in a table. I just glance into the table at the right place to see if it has a function called "reaction" and if it does, I run it.