Re: [Library] anim8 - An animation library - v2.3.0 released
Posted: Fri Feb 15, 2019 4:42 am
How does one get the frame duration of a specific frame in animation? Let's say i have a 6-frame animation:kikito wrote: ↑Tue Jan 22, 2019 12:28 pm The "current frame" is not documented on purpose, because you should not tie your game logic to your animations.
If you must know, it is `anim.position`. Let me explain why you should not use it.
As an example, let's say that your 4-frame animation is a "shooting a gun" animation, with frame durations {0.1, 0.2, 0.3, 0.4 }. Let's say that a bullet should be created a the beginning of the third frame.
The way to properly resolve this is: when you initialize the animation, you also set an independent timer variable to 0. Every time you update the animation with dt, you also increase the timer animation with dt. Then you check if the timer is >= 0.3 seconds (which is the duration of the first and second frames put together, 0.1 + 0.2). If the condition succeeds, you create the bullet and discard the counter.
You should *not* be doing is `if anim.position == 3` on every frame and create the bullet with that if. For two reasons:
- anim.position is going to be equal to 3 for potentially a lot of frames. You will need an extra variable to control that you have already fired the bullet. So you might as well create the timer variable instead of the `hasAlreadyFired` variable.
- The game could have spent a lot of time on the previous frame (doing something intensive like physics or pathfinding) and the animation could have gone from frame 1 to frame 4 without going over the intermediate frames. If you only look at the animation frames you won't create a bullet, but if you use a separate timer you will.
Code: Select all
local grid = Anim8.newGrid(101, 360, sheet:getWidth(), sheet:getHeight())
local obj_anim = Anim8.newAnimation(grid('1-6', 1), 0.3)
Code: Select all
--every time the animation is played/created
local timer = 0
--on update
timer = timer + dt
obj_anim:update(dt)
if timer >= ???? then
--do something
end