Anim is an easy to use animation player package for Ebitengine v2
import "github.com/setanarut/anim"
Let's declare a global variable for the animation player
var animPlayer *anim.AnimationPlayer
Make new animation player from sprite atlas
spriteSheet := ebiten.NewImageFromImage(img)
animPlayer = anim.NewAnimationPlayer(spriteSheet)
Let's specify the coordinates of the animations for the player states.
The figure shows the coordinates for "run" state. NewAnimationState("run", 0, 32, 32, 32, 8, false, false)
animPlayer.NewAnimationState("idle", 0, 0, 32, 32, 5, false, false).FPS = 5
animPlayer.NewAnimationState("run", 0, 32, 32, 32, 8, false, false)
animPlayer.NewAnimationState("jump", 0, 32*2, 32, 32, 4, false, false)
Let's set the initial state.
animPlayer.SetState("idle")
Update animation player
func (g *Game) Update() error {
animPlayer.Update()
Let's update the states according to the character's movement.
if ebiten.IsKeyPressed(ebiten.KeySpace) {
animPlayer.SetState("jump")
} else if ebiten.IsKeyPressed(ebiten.KeyD) {
animPlayer.SetState("run")
} else if ebiten.IsKeyPressed(ebiten.KeyA) {
animPlayer.SetState("run")
// Flip X axis with *ebiten.DrawImageOptions.GeoM
DIO.GeoM.Scale(-1, 1)
} else {
animPlayer.SetState("idle")
}
Finally let's draw Animation player
func (g *Game) Draw(screen *ebiten.Image) {
screen.DrawImage(animPlayer.CurrentFrame, DIO)
Run demo on your local machine
go run github.com/setanarut/anim/examples/demo@latest