I would have answered before, but I was kindof busy and on saturday the forums were offline (at least for me).
I knew several ways to do this, but I didn't know which one was better until I read Cowinatub's answer:
Cowinatub wrote:I was planning on having the animation speed up and slow down depending on how fast the player is moving.
Cowinatub: There's a way to change the delays of each animation frame - each animation has an attribute called delays (animation.delays), which is just an array of durations. You could manipulate that array and change the speed of each frame however you wanted.
But there is a much better way of doing what you want, without having to go over each frame.
Let me explain: Chances are that you already have a "velocity" somewhere in your code. A number that says how fast the player/enemy/etc is moving. If you don't have a number (i.e. you have two numbers, one for vx and another for vy) you need want to get it somehow (i.e. only using vx and ignoring vy, or getting the
length of the vector). Normally this velocity will come in pixels per second (px/s), although the units will not appear in your code. We can call this "self.velocity".
The next thing you need to determine is what velocity is the "standard" - the one in which the animation "looks good" by default, with no modifications. It should be a number: 20 px/s, 30 px/x, whatever. We can call this "animationVelocity"
Then you need to know "how fast you are moving with regards to the sandard velocity" at any given time. This is also a number. It will be 1 when velocity == animationVelocity. If velocity is twice as fast as the animation, it will be 2. And if it's half, it will be 0.5. In other words, this variable, which we can call velocityCoefficient, is self.velocity / animationVelocity.
Now here is the kick: Every time you update the animation, take the velocityCoefficient, and multiply it by dt before giving it to the animation.
So if before you had this:
Code: Select all
animation:update(dt) -- I do this for all velocities
Now you have this:
Code: Select all
local animationVelocity = 30
...
local velocityCoefficient = self.velocity / animationVelocity
animation:update(dt * velocityCoefficient) -- I update faster/slower depending on my velocity
To simplify things up, I recommend you to set the same animationVelocity for all your animations. Otherwise, you will have to replace animationVelocity with self.animationVelocity, and update it every time you change animations.
I hope this helps, good luck with your project.