Page 1 of 1

How to remove images?

Posted: Fri Nov 21, 2014 5:43 am
by Wittloc

Code: Select all

function love.load()
	freddyA = love.graphics.newImage("431.png")
	staticA = love.graphics.newImage("590.png")
	staticB = love.graphics.newImage("591.png")
	staticC = love.graphics.newImage("592.png")
	staticD = love.graphics.newImage("593.png")
	sound = love.audio.newSource("staticB.wav")
	music = love.audio.newSource("menu.wav")
	sound:play()
	music:play()
end

function love.draw()
	love.graphics.draw(freddyA)
	love.graphics.draw(staticA)
end
I want to remove staticA to put staticB after a few seconds, to make a static effect. How do I remove drawn images?

Re: How to remove images?

Posted: Fri Nov 21, 2014 9:46 am
by DmL
In your love.load() you'll want to assign a variable like "currentImage"

Code: Select all

currentImage = staticA
In your love.draw() you want to draw your currentImage

Code: Select all

love.graphics.draw(currentImage, 0, 0)
Then you'll need a callback to load.update(dt)
There you can assign the current time to a local variable.
Then, in an if statement, you can check to see if a certain amount of time has passed, and if so, you can change your currentImage
Something like:

Code: Select all

love.update(dt) -- The dt is the time which has passed since this was last called.  Probably we don't need this right now.
local oldTime = love.timer.getTime()

if (love.timer.getTime() >= oldTime + 1) then -- I believe the 1 should stand for one second, in this case.  Is the new time 1 second more than the old time?
currentImage = staticB
end
That would switch out A for B after 1 second. To switch back and forth, you would add another if statement in that block. To shuffle through all of the images, add them to a table and interate through it!

Hope this helps. I'm not a programmer, so there is probably a bug, but this is the basic idea.

Re: How to remove images?

Posted: Fri Nov 21, 2014 1:14 pm
by zorg
DmL wrote:

Code: Select all

love.update(dt) -- The dt is the time which has passed since this was last called.  Probably we don't need this right now.
local oldTime = love.timer.getTime()

if (love.timer.getTime() >= oldTime + 1) then -- I believe the 1 should stand for one second, in this case.  Is the new time 1 second more than the old time?
currentImage = staticB
end
That won't work, since the oldTime will be overwritten every frame as well; you want this instead:

Code: Select all

local time = 0
function love.update(dt)
time = time + dt
if time > 1 then
currentImage = staticB
end
end

Re: How to remove images?

Posted: Fri Nov 21, 2014 5:55 pm
by DmL
I'm still learning Lua, but I had a feeling that would be the case. You could also do an

Code: Select all

if (oldTime == nil) then
local oldTime -- etc

Re: How to remove images?

Posted: Fri Nov 21, 2014 8:24 pm
by zorg
DmL wrote:...You could also do an

Code: Select all

if (oldTime == nil) then
local oldTime -- etc
The problem with that is that no one is stopping you from defining a global oldTime variable elsewhere; if it _is_ defined, it will never swap.
...Actually, the bigger issue is that you'd be defining the local inside the if's scope, and not the function's.

Code: Select all

function love.update(dt)
local oldTime
if oldTime == nil then
-- ...even if i could transform this so it seemed to work, i don't know whether only defining oldTime as a local will set it to something, or allow through some hackery what i've written before.

Re: How to remove images?

Posted: Fri Nov 21, 2014 8:24 pm
by Wittloc
zorg wrote:
DmL wrote:

Code: Select all

love.update(dt) -- The dt is the time which has passed since this was last called.  Probably we don't need this right now.
local oldTime = love.timer.getTime()

if (love.timer.getTime() >= oldTime + 1) then -- I believe the 1 should stand for one second, in this case.  Is the new time 1 second more than the old time?
currentImage = staticB
end
That won't work, since the oldTime will be overwritten every frame as well; you want this instead:

Code: Select all

local time = 0
function love.update(dt)
time = time + dt
if time > 1 then
currentImage = staticB
end
end
So, if I wanted it to switch from A to B to C to D then repeat, what would I use?

Re: How to remove images?

Posted: Fri Nov 21, 2014 8:32 pm
by zorg
You could do:

Code: Select all

local t = {}
local current = 1
local delay = 1.0 -- seconds

function love.load()
	t[1] = love.graphics.newImage("590.png")
	t[2] = love.graphics.newImage("591.png")
	t[3] = love.graphics.newImage("592.png")
	t[4] = love.graphics.newImage("593.png")
	-- ...
end

function love.draw()
	love.graphics.draw(t[current], 0, 0)
end

local time = 0
	function love.update(dt)
	time = time + dt
	if time > delay then
		current = (current % #t) + 1 -- increase with wrap, think "i = i + 1" with i always being between 1 and the number of elements inclusive.
		time = time - delay
	end
end
for example.

Re: How to remove images?

Posted: Fri Nov 21, 2014 10:55 pm
by Dattorz
If you want to "erase" an image that has been drawn to the screen, all you have to do is not draw it on the next frame.

Every time the game gets ready to draw, by default the screen will be cleared to the background color by calling love.graphics.clear(). This is done for you and you don't need to call this yourself unless of course you have overwritten love.run().

As other have mentioned, if you want to create the impression of animation, you just change the image that you draw with to something else. If you're stuck on this, there are tutorials and helper libraries that can do this for you.

Re: How to remove images?

Posted: Sat Nov 22, 2014 6:31 am
by DmL
zorg wrote: Actually, the bigger issue is that you'd be defining the local inside the if's
Huh. True, thanks for the pointer.