Page 7 of 9

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

Posted: Tue Oct 18, 2016 4:18 pm
by zorg
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)

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

Posted: Tue Oct 18, 2016 7:14 pm
by Jack Dandy
Thanks for the clear-up. :)

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

Posted: Sat Nov 05, 2016 9:36 am
by piotrek75
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)

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

Posted: Tue Jan 02, 2018 12:34 pm
by Marty
Does anybody know how I can change the duration of a running animation or do I have to create a new animation?

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

Posted: Tue Jan 02, 2018 2:45 pm
by kikito
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.

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

Posted: Tue Jan 02, 2018 4:17 pm
by Marty
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! :awesome:

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

Posted: Fri Jan 26, 2018 1:17 pm
by Timon
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 )

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

Posted: Fri Jan 26, 2018 5:11 pm
by zorg
Timon wrote:
Fri Jan 26, 2018 1:17 pm
...Please explain...
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.

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

Posted: Fri Jan 18, 2019 6:16 pm
by Jack Dandy
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?

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

Posted: Tue Jan 22, 2019 12:28 pm
by kikito
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.