All examples · Sprites and 2D · As Markdown, for AIs

Collisions and tags

What this example shows

Each piece says what it collides with, from its own body, so the shot and the bats are written apart and nothing in the scene has to know both. useCollisionWith(Bat, (bat) => bat.api.hurt(1)) meets only the pieces made from Bat, and TypeScript knows what they return: no check, no cast. useCollisionWith('breakable', …) meets anything carrying the tag, here crates that are plain sprites with no component of their own, tagged with useTag('breakable').add(sprite).

A tag is also how the game finds things of a kind: the bats are made with useSpawn(Bat, { tags: ['enemy'] }), and the HUD counts enemies.all(), which leaves out the ones already destroyed. useCollisionBetween(Ship, Bat, …) is the other way round, a rule of the scene that knows both: the ship flashes while a bat touches it. The arrow keys move, X shoots.

Starting
Starting
—