How to undraw something?

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.
User avatar
legendman3
Citizen
Posts: 68
Joined: Sun Jan 22, 2012 8:29 pm

How to undraw something?

Post by legendman3 »

Like when i call to draw a image:

Code: Select all

love.graphics.draw(trollface, trollx, trolly)
How can i undraw it?
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: How to undraw something?

Post 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/
Kurosuke needs beta testers
User avatar
legendman3
Citizen
Posts: 68
Joined: Sun Jan 22, 2012 8:29 pm

Re: How to undraw something?

Post by legendman3 »

Um... Hehe, how would i stop drawing it?
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: How to undraw something?

Post by tentus »

Here, we just had a thread on this:
viewtopic.php?f=3&t=7664
Kurosuke needs beta testers
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: How to undraw something?

Post 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.
User avatar
legendman3
Citizen
Posts: 68
Joined: Sun Jan 22, 2012 8:29 pm

Re: How to undraw something?

Post by legendman3 »

Tentus, ha guess i didnt search well enough.

Vee, Thanks.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: How to undraw something?

Post 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.
Kurosuke needs beta testers
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: How to undraw something?

Post 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
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: How to undraw something?

Post 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).
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: How to undraw something?

Post 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.
Kurosuke needs beta testers
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests