## [Library] anim8 - An animation library - v2.3.0 released

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

### Re: [Library] anim8 - An animation library - v2.3.0 released

Jack Dandy wrote:In the readme in https://github.com/kikito/anim8, it says:
anim8 readme wrote:onLoop is an optional parameter which can be either a function or a string representing one of the animation methods. It does nothing by default. If specified, it will be called every time an animation "loops".It will have two parameters: the animation instance, and how many loops have been elapsed. The most usual value (apart from none) is the string 'pauseAtEnd'. It will make the animation loop once and then pause and stop on the last frame.
Now, let's say I supply my own little function.
But I don't exactly understand, how am I supposed to access the two parameters you talked about in the quote?

For example, the following didn't work.

Code: Select all

slashAnim = anim8.newAnimation(animgrid('1-6',1), 0.04, (function(a,b) DRAWSLASH=false  a:pauseAtEnd() end))
So, these are legal (I'm only going to modify the third parameter):

Code: Select all

	someAnim1 = anim8.newAnimation(animgrid('1-6',1), 0.04) -- or an explicit nil, since it's optional.
someAnim2 = anim8.newAnimation(animgrid('1-6',1), 0.04, 'pauseAtEnd') -- this is the string version.

The readme goes on to list the animation methods (detailed blocks removed), probably not a good idea to use some/most of them though:
Animations have the following methods:
animation:update(dt)
animation:draw(image, x,y, angle, sx, sy, ox, oy, kx, ky)
animation:gotoFrame(frame)
animation:pause()
animation:resume()
animation:clone()
animation:flipH()
animation:flipV()
animation:pauseAtEnd() --Moves the animation to its last frame and then pauses it.
animation:pauseAtStart() --Moves the animation to its first frame and then pauses it.
animation:getDimensions()
(...)

The third type would be a function:

Code: Select all

	someAnim3 = anim8.newAnimation(animgrid('1-6',1), 0.04, function(animationInstance,elapsedLoops) --[[do something with animInstance using elapsedLoops...--]] end)
-- or alternatively:
local function animCallback(instance,loops)
-- something
end
someAnim3 = anim8.newAnimation(animgrid('1-6',1), 0.04, animCallback)

Me and my stuff True 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.

Jack Dandy
Prole
Posts: 49
Joined: Mon Sep 08, 2014 4:26 pm

### Re: [Library] anim8 - An animation library - v2.3.0 released

Thanks for the clear-up.

piotrek75
Prole
Posts: 9
Joined: Tue Oct 18, 2016 7:43 pm

### Re: [Library] anim8 - An animation library - v2.3.0 released

Hello, FYI, I could not understand how to get a CURRENT frame count and TOTAL frame count for an animation.

So I modified a bit the anim8.lua by adding:

Code: Select all

function Animation:getCurrentFrameCounter()
return self.position
end

function Animation:getTotalFrameCounter()
return #self.frames
end
(Posting this here as it may be usefull for some of you and I still do not get the process of having a change validated and pushed to the main anim8 GitHub repository)

Marty
Citizen
Posts: 89
Joined: Mon Dec 04, 2017 1:47 am
Location: Germany

### Re: [Library] anim8 - An animation library - v2.3.0 released

Does anybody know how I can change the duration of a running animation or do I have to create a new animation?

───▄▀▀▀▄▄▄▄▄▄▄▀▀▀▄───
───█▒▒░░░░░░░░░▒▒█───
────█░░░░░░░░░█────
▄▄──█░░░▀█▀░░░█──▄▄
█░░█▀▄░░░░░░░▄▀█░░█

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: [Library] anim8 - An animation library - v2.3.0 released

You can make animations go faster or slower by altering the dt you pass them:

Code: Select all

-- if instead of anim:update(dt) you do:
anim:update(dt * 2)   -- anim will run twice as fast.
anim:update(dt * 0.5) -- anim will run twice as slow.

So you can store that coefficient in a variable and multiply it on the update section. You can use this to do cool stuff like time manipulation, or make the gears of a train engine move in synchronicity with its velocity, using a single animation.

Apart from that, you can always create a new animation, of course.
When I write def I mean function.

Marty
Citizen
Posts: 89
Joined: Mon Dec 04, 2017 1:47 am
Location: Germany

### Re: [Library] anim8 - An animation library - v2.3.0 released

kikito wrote:
Tue Jan 02, 2018 2:45 pm
You can make animations go faster or slower by altering the dt you pass them:

Code: Select all

-- if instead of anim:update(dt) you do:
anim:update(dt * 2)   -- anim will run twice as fast.
anim:update(dt * 0.5) -- anim will run twice as slow.

So you can store that coefficient in a variable and multiply it on the update section. You can use this to do cool stuff like time manipulation, or make the gears of a train engine move in synchronicity with its velocity, using a single animation.

Apart from that, you can always create a new animation, of course.
Yet so simple, how could I miss that, great. Thank you so much!

───▄▀▀▀▄▄▄▄▄▄▄▀▀▀▄───
───█▒▒░░░░░░░░░▒▒█───
────█░░░░░░░░░█────
▄▄──█░░░▀█▀░░░█──▄▄
█░░█▀▄░░░░░░░▄▀█░░█

Timon
Prole
Posts: 3
Joined: Fri Jan 26, 2018 12:08 pm

### Re: [Library] anim8 - An animation library - v2.3.0 released

Dear Kikito, thanks you for anim8!

Please explain - in Animations part i see
onLoop is an optional parameter which can be a function or a string representing one of the animation methods. It does nothing by default. If specified, it will be called every time an animation "loops". It will have two parameters: the animation instance, and how many loops have been elapsed. The most usual value (apart from none) is the string 'pauseAtEnd'. It will make the animation loop once and then pause and stop on the last frame.
But bellow i see another description
animation:pauseAtEnd()
Moves the animation to its last frame and then pauses it.
I know that this works as described below - moves the animation to its last frame and then pauses it.

Thanks you again )

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

### Re: [Library] anim8 - An animation library - v2.3.0 released

Timon wrote:
Fri Jan 26, 2018 1:17 pm
Those two sentences mean the same thing, playing the animation once == looping once, then pausing at the last frame, i.e. the index won't go back to the first frame.
Me and my stuff True 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.

Jack Dandy
Prole
Posts: 49
Joined: Mon Sep 08, 2014 4:26 pm

### Re: [Library] anim8 - An animation library - v2.3.0 released

Hey there. Let's say I have an animation composed of 4 frames. How can I figure out the number of the frame the animation is currently in?

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: [Library] anim8 - An animation library - v2.3.0 released

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.
When I write def I mean function.

### Who is online

Users browsing this forum: No registered users and 5 guests