Expand description

Animation management

To create custom animation and use it you will need an image that can be represented as tiles. Each tile contains specific frame of animation. Every specific animation should be placed in separate row.

§Examples

Let’s say we have an image of our character where every frame is 15x20 rect and we have this animations:

  • Idle animation with 20 frames at 12 fps
  • Run animation with 15 frames at 15 fps
use macroquad::experimental::animation::*;
use macroquad::prelude::*;

#[macroquad::main("Animation")]
async fn main() {
    // Define animations
    let mut sprite = AnimatedSprite::new(
        15,
        20,
        &[
            Animation {
                name: "idle".to_string(),
                row: 0,
                frames: 20,
                fps: 12,
            },
            Animation {
                name: "run".to_string(),
                row: 1,
                frames: 15,
                fps: 15,
            },
        ],
        true,
    );
    let image = load_texture("some_path.png").await.unwrap();
    loop {
        clear_background(WHITE);
        // Now we can draw our character
        draw_texture_ex(
            image,
            10.,
            10.,
            WHITE,
            DrawTextureParams {
                source: Some(sprite.frame().source_rect),
                dest_size: Some(sprite.frame().dest_size),
                ..Default::default()
            }
        );
        // Update frame
        sprite.update();
        next_frame().await;
    }
}

Structs§