Drawing an 'undrawing' 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
Buby
Prole
Posts: 15
Joined: Mon Dec 07, 2015 4:28 am

Drawing an 'undrawing' images

Post by Buby »

Hey Guys! I have recently been having troubles in my script (as in it has been taking too much space). So my problem is when i want to play an animation i use a method of teleporting the animation or sprite to the previous x and y. for example...

Code: Select all

require("AnAL")
love.graphics.setBackgroundColor(255,255,255)
health = 5

y5 = -600
y4 = -600
y3 = -600
y2 = -600
y1 = -600
y0 = -600

function love.load()	
	health5 = love.graphics.newImage("assets/heartline5.png")
	health4 = love.graphics.newImage("assets/heartline4.png")
	health3 = love.graphics.newImage("assets/heartline3.png")
	health2 = love.graphics.newImage("assets/heartline2.png")
	health1 = love.graphics.newImage("assets/heartline1.png")
	health0 = love.graphics.newImage("assets/heartline0.png")
end

function love.update(dt)
	
--health
if health == 5 then
	y5 = 20
	y4 = -600
	y3 = -600
	y2 = -600
	y1 = -600
	y0 = -600
elseif health == 4 then
	y5 = -600
	y4 = 20
	y3 = -600
	y2 = -600
	y1 = -600
	y0 = -600
elseif health == 3 then
	y5 = -600
	y4 = -600
	y3 = 20
	y2 = -600
	y1 = -600
	y0 = -600
elseif health == 2 then
	y5 = -600
	y4 = -600
	y3 = -600
	y2 = 20
	y1 = -600
	y0 = -600
elseif health == 1 then
	y5 = -600
	y4 = -600
	y3 = -600
	y2 = -600
	y1 = 20
	y0 = -600
elseif health == 0 then
	y5 = -600
	y4 = -600
	y3 = -600
	y2 = -600
	y1 = -600
	y0 = 20
end
--health
end

function love.draw()
	love.graphics.draw(health5, 800, y5)
	love.graphics.draw(health4, 800, y4)
	love.graphics.draw(health3, 800, y3)
	love.graphics.draw(health2, 800, y2)
	love.graphics.draw(health1, 800, y1)
	love.graphics.draw(health0, 800, y0)
end
So my question is, is there a quicker and more efficient way of doing this, like "undrawing" the image or animation and then recalling it? instead of just teleporting it off screen? this would make my scripts much more efficient and easier to navigate

--note i am fairy new to love

Code: Select all

if 2+2 == 5 then
   love.event.push('quit')
end
User avatar
Fenrir
Party member
Posts: 222
Joined: Wed Nov 27, 2013 9:44 am
Contact:

Re: Drawing an 'undrawing' images

Post by Fenrir »

Just don't call love.graphics.draw on an image you don't need anymore and it won't be displayed.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Drawing an 'undrawing' images

Post by zorg »

You can put the images into a table, and only draw one image at once.

Code: Select all

local health
love.load()
  health = {}
  -- even this could be written with less duplication as a cycle (a for loop)
  health[0] = love.graphics.newImage("assets/heartline0.png") 
  health[1] = love.graphics.newImage("assets/heartline1.png")
  health[2] = love.graphics.newImage("assets/heartline2.png") 
  health[3] = love.graphics.newImage("assets/heartline3.png") 
  health[4] = love.graphics.newImage("assets/heartline4.png") 
  health[5] = love.graphics.newImage("assets/heartline5.png")  
end

local timer = 0.0
local frame = 0
function love.update(dt)
  timer = timer + dt
  if timer >= 1.0 then
    frame = (frame + 1) % 6 -- or use a variable, like totalFrames or something
    timer = timer - 1.0
  end
end

function love.draw()
  love.graphics.draw(health[frame], 800, 20)
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.
Buby
Prole
Posts: 15
Joined: Mon Dec 07, 2015 4:28 am

Re: Drawing an 'undrawing' images

Post by Buby »

zorg wrote:You can put the images into a table, and only draw one image at once.

Code: Select all

local health
love.load()
  health = {}
  -- even this could be written with less duplication as a cycle (a for loop)
  health[0] = love.graphics.newImage("assets/heartline0.png") 
  health[1] = love.graphics.newImage("assets/heartline1.png")
  health[2] = love.graphics.newImage("assets/heartline2.png") 
  health[3] = love.graphics.newImage("assets/heartline3.png") 
  health[4] = love.graphics.newImage("assets/heartline4.png") 
  health[5] = love.graphics.newImage("assets/heartline5.png")  
end

local timer = 0.0
local frame = 0
function love.update(dt)
  timer = timer + dt
  if timer >= 1.0 then
    frame = (frame + 1) % 6 -- or use a variable, like totalFrames or something
    timer = timer - 1.0
  end
end

function love.draw()
  love.graphics.draw(health[frame], 800, 20)
end
haii again!, Thanks for the code, just wondering with this code ,will it delete the picture after use (as in another one is called)

Code: Select all

if 2+2 == 5 then
   love.event.push('quit')
end
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Drawing an 'undrawing' images

Post by s-ol »

Don't mean to offend, but you don't need advanced tutorials showing techniques, you need a beginner's Lua tutorial.
Take a look at the free "Programming in Lua" guide!
Last edited by s-ol on Thu Jan 07, 2016 4:20 pm, edited 1 time in total.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Drawing an 'undrawing' images

Post by zorg »

Buby wrote:haii again!, Thanks for the code, just wondering with this code ,will it delete the picture after use (as in another one is called)
If you mean will it only show one image at once, then yes.
If you mean will it unload all the other images, then no, but since you're gonna animate using them, why load and unload them all the time? That's slow and unnecessary. :3
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.
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Drawing an 'undrawing' images

Post by bobbyjones »

Also AnAl is outdated. You probably should use anim8
Buby
Prole
Posts: 15
Joined: Mon Dec 07, 2015 4:28 am

Re: Drawing an 'undrawing' images

Post by Buby »

S0lll0s wrote:Don't mean to offend, but you don't need advanced tutorials showing techniques, you need a beginner's Lua tutorial.
Take a look at the free "Programming in Lua" guide!
I agree with you on some terms but i do know the basics and was looking for how to improve on my techniques maybe i just worded it wrong

Code: Select all

if 2+2 == 5 then
   love.event.push('quit')
end
Buby
Prole
Posts: 15
Joined: Mon Dec 07, 2015 4:28 am

Re: Drawing an 'undrawing' images

Post by Buby »

zorg wrote:
Buby wrote:haii again!, Thanks for the code, just wondering with this code ,will it delete the picture after use (as in another one is called)
If you mean will it only show one image at once, then yes.
If you mean will it unload all the other images, then no, but since you're gonna animate using them, why load and unload them all the time? That's slow and unnecessary. :3
oh okay i get it now, ive always wondered how to use tables, I will try to learn more about them

Code: Select all

if 2+2 == 5 then
   love.event.push('quit')
end
garcia1000
Prole
Posts: 34
Joined: Sat Nov 28, 2015 5:54 am

Re: Drawing an 'undrawing' images

Post by garcia1000 »

Buby wrote:
S0lll0s wrote:Don't mean to offend, but you don't need advanced tutorials showing techniques, you need a beginner's Lua tutorial.
Take a look at the free "Programming in Lua" guide!
I agree with you on some terms but i do know the basics and was looking for how to improve on my techniques maybe i just worded it wrong
hello :awesome:

Tables are very useful in lua. If you learn more about them, your programming power will increase 100x
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests