Page 1 of 1

trouble making an animation

Posted: Sat Jun 10, 2023 4:38 pm
by NewDeveloper:D
Hello, good morning, I'm new to the developer forums, and I have a question about a code that I'm doing, more specifically it's an animation

function love.load()
fragmentos = 0
animacion = false
end

function love.update(dt)
if animacion == true then
fragmentos = fragmentos + 1 * dt
end
end

function.love.draw()
fragmentos = math.floor(fragmentos)
if animacion == true then
for i = 0, 6 do
if fragmentos == 1 then
love.graphics.draw(tragamonedas1, 0, 0)
fragmentos = 1
elseif fragmentos == 2 then
love.graphics.draw(tragamonedas2, 0, 0)
fragmentos = 2
elseif fragmentos == 3 then
love.graphics.draw(tragamonedas3, 0, 0)
elseif fragmentos == 4 then
love.graphics.draw(tragamonedas4, 0, 0)
elseif fragmentos == 5 then
love.graphics.draw(tragamonedas5, 0, 0)
elseif fragmentos == 6 then
love.graphics.draw(tragamonedas6, 0, 0)
elseif fragmentos == 7 then
love.graphics.draw(tragamonedas7, 0, 0)
elseif fragmentos == 8 then
love.graphics.draw(tragamonedas8, 0, 0)
elseif fragmentos == 9 then
love.graphics.draw(tragamonedas9, 0, 0)
elseif fragmentos == 10 then
love.graphics.draw(tragamonedas10, 0, 0)
elseif fragmentos == 11 then
love.graphics.draw(tragamonedas11, 0, 0)
elseif fragmentos == 12 then
love.graphics.draw(tragamonedas12, 0, 0)
elseif fragmentos == 13 then
love.graphics.draw(tragamonedas13, 0, 0)
elseif fragmentos == 14 then
love.graphics.draw(tragamonedas14, 0, 0)
elseif fragmentos == 15 then
love.graphics.draw(tragamonedas15, 0, 0)
elseif fragmentos == 16 then
love.graphics.draw(tragamonedas16, 0, 0)
elseif fragmentos == 17 then
love.graphics.draw(tragamonedas17, 0, 0)
elseif fragmentos == 18 then
love.graphics.draw(tragamonedas18, 0, 0)
elseif fragmentos == 19 then
love.graphics.draw(tragamonedas19, 0, 0)
elseif fragmentos == 20 then
love.graphics.draw(tragamonedas20, 0, 0)
elseif fragmentos == 21 then
love.graphics.draw(tragamonedas21, 0, 0)
elseif fragmentos == 22 then
love.graphics.draw(tragamonedas22, 0, 0)
elseif fragmentos == 23 then
love.graphics.draw(tragamonedas23, 0, 0)
elseif fragmentos == 24 then
love.graphics.draw(tragamonedas24, 0, 0)
fragmentos = 0
end
end
end
end

My problem is that it has to repeat the animation 6 times, but it does it infinitely, or when I manage to make it stop it only does it once and it does it too fast. Could anyone help me please. Thanks

Re: trouble making an animation

Posted: Sat Jun 10, 2023 8:50 pm
by dusoft
There are many issues with your code, let's start cleaning it up:
1) use an object (table) to keep your animations in, so you can do:

Code: Select all

if tragamonedas[fragmentos]~=nil then
   love.graphics.draw(tragamonedas[fragmentos], 0, 0)
end
Also, you might not get an integer when using delta time, so you should either round (or ceil) or test within some interval. Also looping in draw will just flash / draw over in quick succession, so you will just possibly see the last image drawn only. I am not sure what the looping is about since you just need to use delta time.

Re: trouble making an animation

Posted: Sat Jun 10, 2023 10:59 pm
by BrotSagtMist
This is how i do animations

Code: Select all

love.update=function(dt)
 counter=counter+dt*speed
end
love.draw=function()
 love.graphics.draw(pictures[math.ceil(counter%#pictures)])
end
Firstoff your time value needs to be taken of dt, this is the only way to ensure the animation (or the whole game for the matter) runs at the same speed on every computer.
We will multiply this by speed to get a fluent animation, if this value is say 60 it means the animation plays at 60 fps.
Next all the pictures are neatly stuffed into a numbered table.
Then we have: math.ceil will round the value up to the nearest integer.
% is the modulo operand, this will cause the number to loop once it hits the operand.
# simply means the size of the table.

Re: trouble making an animation

Posted: Sat Jun 10, 2023 11:25 pm
by BrotSagtMist
Of course i was completely ignoring the 6 times repeat thing:

Code: Select all

love.draw=function()
 if counter<(#pictures*6) then
  love.graphics.draw(pictures[math.ceil(counter%#pictures)])
 end
end
Should do it, but it is not a fine solution for bigger games.

Re: trouble making an animation

Posted: Sun Jun 11, 2023 3:32 am
by NewDeveloper:D
oh thank you very much you are a very good developer, I hope that when I have more problems you can help me, thank you