Elements, Sprites & Animation
A game world has dozens of moving things—heroes, enemies, coins, trees, and floating shadows.
Instead of tracking hundreds of loose x and y variables, Maverick packages each thing
into a self-contained GameElement with its own artwork and flipbook animations.
Without GameElement & Spritesheets
Tracking playerX, enemy1X, enemy2X in separate arrays quickly turns into spaghetti code.
Loading 50 separate PNG files for every walk frame causes flickering while images download, and manually writing
frame-timer math for every character is tedious and error-prone.
Why Maverick Built These Tools
GameElement gives every object its own Config (where it is, how it's rotated/flipped) and
State (health, speed). SpriteManager preloads one combined image (a spritesheet)
and slices out frames, while AnimationPlayer flips through them like a real flipbook.
1. Creating a GameElement
Think of a GameElement like an actor on stage: it has a script for what to do each tick
(onUpdate) and how to look (onDraw). Keeping Config (position, rotation, transparency)
separate from State (speed, HP) keeps your data tidy and easy to save later.
import { Game, GameElement } from 'maverick';
class Player extends GameElement {
onUpdate(_game: Game, timeDelta: number): void {
this.Config.pos!.x += this.State.speed * timeDelta;
}
onDraw(game: Game): void {
game.Viewport.drawElement(this);
}
}
// Inside Game.onReady():
const player = new Player(
this.Logger,
{
name: 'hero',
sprite: 'heroIdle',
pos: { x: 40, y: 120 },
rotation: 0,
flipX: false,
alpha: 1,
zIndex: 1,
},
{ speed: 120 }
);
this.Elements.add(player);
2. Spritesheets & Preloading
A spritesheet is like a sticker sheet: instead of downloading 20 separate image files from the internet
(which slows down loading and causes invisible characters), you put all poses into one image.
Sprites.addSource() registers the file, await Sprites.preload() waits until it's finished downloading,
and Sprites.addSprite() cuts out each sticker by its coordinates.
this.Sprites.addSource('sheet', './assets/characters.png');
await this.Sprites.preload('sheet'); // 'idle' | 'loading' | 'ready' | 'error'
this.Sprites.addSprite('heroWalk1', 'sheet', {
source: 'sheet',
spriteCoordinates: { x: 0, y: 0, width: 32, height: 32 },
renderOffset: { x: -16, y: -16 },
scale: 2,
});
3. Frame Animation (AnimationPlayer)
Think of AnimationPlayer as a flipbook controller. You define named clips—like a looping
'walk' cycle or a one-shot 'attack' swing—and tell it how many frames per second (fps)
to flip. When a one-shot attack finishes, onComplete automatically switches you back to walking.
import { AnimationPlayer } from 'maverick';
const anim = new AnimationPlayer();
anim.addClip({ id: 'walk', frames: ['heroWalk1', 'heroWalk2'], fps: 8, loop: true });
anim.addClip({ id: 'attack', frames: ['heroAtk1', 'heroAtk2'], fps: 12, loop: false });
anim.onComplete = (clipId) => {
if (clipId === 'attack') anim.play('walk');
};
anim.play('walk');
// Inside element.onUpdate(game, dt):
anim.update(dt);
this.Config.sprite = anim.CurrentFrame ?? this.Config.sprite;
4. Parent Hierarchy (Attaching Things Together)
Imagine a knight holding a shield, or a floating pet following your hero. Without parent-child links,
you'd have to manually copy the hero's coordinates to the pet every single frame.
Instead, set parent: 'hero' on the child element and call Elements.getWorldPosition(child)—its
offset automatically moves with the hero!
GameElement, AnimationPlayer & Parent Hierarchy