I'm designing an animation system for a game engine and find myself looking at two different methods for updating the frame count. Both are equally valid and provide the same result, advancing to the next frame, and either looping through, or holding at the end as specified by the animation's property.
My first pass
My second shot
I'm curious as to which people find easier to understand, and which I should ultimately implement. and for the record, yes this is JavaScript, assets is a module scoped variable that contains the asset objects and data. `this` is a local reference file to be handed out to the game objects referencing the art asset and tracking state.
My first pass
Code:
if (this.currentFrame < assets[this.id][this.animation].sequence.length - 1) {
this.currentFrame++;
} else if (assets[this.id][this.animation.repeat) {
this.currentFrame = 0;
}
Code:
this.currentFrame++;
if (this.currentFrame === assets[this.id][this.animation].sequence.length) {
if (assets[this.id][this.animation].repeat) {
this.currentFrame = 0;
} else {
this.currentFrame--;
}
}