Page 1 of 1

Drawing an 'undrawing' images

Posted: Thu Jan 07, 2016 1:41 pm
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

Re: Drawing an 'undrawing' images

Posted: Thu Jan 07, 2016 2:30 pm
by Fenrir
Just don't call love.graphics.draw on an image you don't need anymore and it won't be displayed.

Re: Drawing an 'undrawing' images

Posted: Thu Jan 07, 2016 2:36 pm
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

Re: Drawing an 'undrawing' images

Posted: Thu Jan 07, 2016 2:40 pm
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)

Re: Drawing an 'undrawing' images

Posted: Thu Jan 07, 2016 2:52 pm
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!

Re: Drawing an 'undrawing' images

Posted: Thu Jan 07, 2016 3:43 pm
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

Re: Drawing an 'undrawing' images

Posted: Thu Jan 07, 2016 8:18 pm
by bobbyjones
Also AnAl is outdated. You probably should use anim8

Re: Drawing an 'undrawing' images

Posted: Fri Jan 08, 2016 1:19 am
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

Re: Drawing an 'undrawing' images

Posted: Fri Jan 08, 2016 1:22 am
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

Re: Drawing an 'undrawing' images

Posted: Fri Jan 08, 2016 9:12 am
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