How to remove images?

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.
Post Reply
Wittloc
Prole
Posts: 3
Joined: Fri Nov 21, 2014 5:41 am

How to remove images?

Post by Wittloc » Fri Nov 21, 2014 5:43 am

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?

User avatar
DmL
Prole
Posts: 18
Joined: Fri Nov 21, 2014 9:09 am

Re: How to remove images?

Post by DmL » Fri Nov 21, 2014 9:46 am

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.

User avatar
zorg
Party member
Posts: 3055
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to remove images?

Post by zorg » Fri Nov 21, 2014 1:14 pm

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
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

User avatar
DmL
Prole
Posts: 18
Joined: Fri Nov 21, 2014 9:09 am

Re: How to remove images?

Post by DmL » Fri Nov 21, 2014 5:55 pm

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

User avatar
zorg
Party member
Posts: 3055
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to remove images?

Post by zorg » Fri Nov 21, 2014 8:24 pm

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.
Last edited by zorg on Fri Nov 21, 2014 8:25 pm, edited 1 time in total.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

Wittloc
Prole
Posts: 3
Joined: Fri Nov 21, 2014 5:41 am

Re: How to remove images?

Post by Wittloc » Fri Nov 21, 2014 8:24 pm

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?

User avatar
zorg
Party member
Posts: 3055
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to remove images?

Post by zorg » Fri Nov 21, 2014 8:32 pm

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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

User avatar
Dattorz
Citizen
Posts: 66
Joined: Sat Oct 27, 2012 12:32 am
Contact:

Re: How to remove images?

Post by Dattorz » Fri Nov 21, 2014 10:55 pm

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.

User avatar
DmL
Prole
Posts: 18
Joined: Fri Nov 21, 2014 9:09 am

Re: How to remove images?

Post by DmL » Sat Nov 22, 2014 6:31 am

zorg wrote: Actually, the bigger issue is that you'd be defining the local inside the if's
Huh. True, thanks for the pointer.

Post Reply

Who is online

Users browsing this forum: No registered users and 34 guests