Tags
Volver a la referencia

Tags

What something is, as many as it likes: finding the things of a kind, and colliding with them.

3 símbolos

useTagfunctionhooknacatamalon

useTag(name: string) => TTag

Gives the tag name, to put on things, take off, ask about and find: what something is, 'enemy', 'coin', 'damageable', as many as it likes. Asking is getting: two pieces that ask for 'enemy' get the same tag, and nothing registers it.

Asked for in a body, like every hook; what it gives works whenever, from a frame or a callback. It finds in the scene it was asked for in. Collisions are asked by tag too: useCollisionWith(enemies, ...) or useCollisionWith('enemy', ...).

Parámetros

namestring

The tag.

Ejemplo

const Enemy = () => {
    useTag('enemy').add(useSelf());
    createSprite({ key: 'bat' });
};

export const Level: TSceneFn = () => {
    const coins = useTag('coin');
    coins.add(createSprite({ key: 'coin', transform: { x: 80, y: 60 } }));
    useUpdate(() => {
        if (coins.all().length === 0) console.log('all taken');
    });
    return createScene();
};
Ver el código · hooks/tags/use_tag.ts:40

TTagtypenacatamalon

A tag, as useTag gives it: one name, and what to do with everything that carries it.

The name is the tag: two pieces that ask for 'enemy' get the same one, and nothing registers it. What carries it carries it as data (thing.tags), so this and the field are never out of step.

Propiedades

namestring
add
remove
has
all
clear
Ver el código · tags/tags.ts:28