https://github.com/setanarut/anim
Animation player for Ebitengine v2
https://github.com/setanarut/anim
Last synced: 5 months ago
JSON representation
Animation player for Ebitengine v2
- Host: GitHub
- URL: https://github.com/setanarut/anim
- Owner: setanarut
- License: cc0-1.0
- Created: 2024-09-20T18:36:19.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-16T04:32:43.000Z (over 1 year ago)
- Last Synced: 2025-04-29T23:12:55.907Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 38.1 KB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://pkg.go.dev/github.com/setanarut/anim)
# Anim

Anim is an easy to use animation player package for Ebitengine v2
```Go
import "github.com/setanarut/anim"
```
## Tutorial
Let's declare a global variable for the animation player
```Go
var animPlayer *anim.AnimationPlayer
```
Make new animation player from sprite atlas

```Go
spriteSheet := anim.Atlas{
Name: "Default",
Image: 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.

```Go
animPlayer.NewAnim("idle", 0, 0, 32, 32, 5, false, false, 5)
animPlayer.NewAnim("run", 0, 32, 32, 32, 8, false, false, 12)
animPlayer.NewAnim("jump", 0, 32*2, 32, 32, 4, false, false, 15)
```
Let's set the initial animation state.
```Go
animPlayer.SetAnim("idle)
```
Update animation player
```Go
func (g *Game) Update() error {
animPlayer.Update()
```
Let's update the states according to the character's movement.
```Go
if ebiten.IsKeyPressed(ebiten.KeySpace) {
animPlayer.SetAnim("jump")
} else if ebiten.IsKeyPressed(ebiten.KeyD) {
animPlayer.SetAnim("run")
} else if ebiten.IsKeyPressed(ebiten.KeyA) {
animPlayer.SetAnim("run")
// FlipX
DIO.GeoM.Scale(-1, 1)
// Align to zero
DIO.GeoM.Translate(float64(animPlayer.CurrentFrame.Bounds().Dx()), 0)
} else {
animPlayer.SetAnim("idle")
}
```
Finally let's draw Animation player
```Go
func (g *Game) Draw(screen *ebiten.Image) {
screen.DrawImage(animPlayer.CurrentFrame, DIO)
```
## Examples
### Simple demo
Run [demo](./examples/demo/) on your local machine
```zsh
go run github.com/setanarut/anim/examples/demo@latest
```
### Multiple sprite sheet example
Example of alternative sprite sheets with exactly the same coordinates.
Run [atlases](./examples/atlases/) on your local machine;
```zsh
go run github.com/setanarut/anim/examples/atlases@latest
```