Advanced • Lesson 6

Canvas UI, Web Audio, Timers & Persistence

Build menus and HUDs with game.UI and wrapped text, route sound effects and music through Web Audio buses, animate values with pause-aware Timers/Tweens, emit particles, and save game slots.

1. Retained Canvas UI & Wrapped Text

Widgets added to game.UI automatically intercept pointer and focused keyboard events before gameplay handlers fire.

import { UIButton, UILabel, UIPanel, UIProgressBar, UIStack } from 'maverick';

const stack = new UIStack({
  id: 'hudStack',
  bounds: { x: 16, y: 16, width: 220, height: 140 },
  spacing: 10,
  padding: 12,
});
stack.addChild(new UILabel({ id: 'title', bounds: { x: 0, y: 0, width: 196, height: 24 }, text: 'RPG Store' }));
stack.addChild(new UIProgressBar({ id: 'hp', bounds: { x: 0, y: 0, width: 196, height: 14 }, value: 0.75 }));
stack.addChild(new UIButton({
  id: 'buyBtn',
  bounds: { x: 0, y: 0, width: 196, height: 36 },
  label: 'Buy Potion (25g)',
  onClick: () => this.Sounds.play('coin', { bus: 'sfx' }),
}));

this.UI.add(new UIPanel({ id: 'hudBg', bounds: { x: 16, y: 16, width: 220, height: 140 }, zIndex: 0 }));
this.UI.add(stack);

2. Web Audio Buses (master, sfx, music)

this.Sounds.addSource('coin', './assets/coin.wav');
this.Sounds.addSource('bgm', './assets/town.ogg');
await this.Sounds.preload();

this.Sounds.setBusVolume('sfx', 0.8);
this.Sounds.setBusVolume('music', 0.4);
await this.Sounds.playMusic('bgm');
await this.Sounds.play('coin', { bus: 'sfx' });

3. Pause-Aware Timers, Tweens, Signals & Particles

import { Easing, ParticleEmitter, Signal } from 'maverick';

const onGoldChanged = new Signal<number>();
onGoldChanged.connect((gold) => console.log('Gold:', gold));

this.Timers.after(1.5, () => onGoldChanged.emit(100));
this.Tweens.to(cardPos, { y: 40 }, 0.4, Easing.easeOutQuad);

const sparks = new ParticleEmitter({ maxParticles: 60, lifetime: 0.4, gravityY: 200 });
sparks.emit(120, 80, 15);

4. Asset Manifest & Versioned Save Slots

import { AssetManifestLoader, SaveManager } from 'maverick';

await AssetManifestLoader.load(
  { images: { hero: './hero.png' }, sounds: { coin: './coin.wav' } },
  this.Sprites,
  this.Sounds,
  (p) => console.log(`Loading: ${Math.round(p.progress * 100)}%`)
);

const saves = new SaveManager('my_rpg', 1);
saves.save('slot1', { level: 3, gold: 250 });
const loaded = saves.load<{ level: number; gold: number }>('slot1');