# HUD with launch

> useScene().launch runs a HUD scene alongside the gameplay one, in parallel, instead of replacing it.

Example `hud-launch`, running at https://nacatamalon.com/en/examples/hud-launch. The whole catalogue: https://nacatamalon.com/llms/examples.md.

`useScene().launch(name)` runs a second scene in parallel instead of replacing the current one, which is what a HUD or an overlay wants. The gameplay scene bounces a sprite while the launched HUD scene keeps a bar pinned on top.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { Game } from './scenes/game';
import { Hud } from './scenes/hud';
import { WIDTH, HEIGHT } from './settings';

const game = createGame('#app', {
    width: WIDTH,
    height: HEIGHT,
    background: '#0f1b2d'
});

// Declare Scenes
game({
    Game,
    Hud
});
```

`components/healthbar.ts`:

```ts
import { createSprite, useUpdate } from 'nacatamalon';
import * as Settings from '../settings';

export const HealthBar = () => {
    createSprite({
        width: 168,
        height: 22,
        tint: {
            r: 1,
            g: 1,
            b: 1,
            a: 0.25
        },
        transform: {
            x: 96,
            y: 24,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    const segments = [0, 1, 2, 3, 4].map((i) => createSprite({
        width: 26,
        height: 12,
        tint: '#4ade80',
        transform: {
            x: 30 + i * 33,
            y: 24,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    }));

    // Losing a segment a second, then full again: its own clock, unaware of anything below.

    useUpdate((_delta, time) => {
        const left = 5 - Math.floor(time % 6);

        segments.forEach((segment, i) => {
            segment.tint = i < left ? Settings.HEALTH : Settings.EMPTY;
        });
    });
};
```

`components/player.ts`:

```ts
import { createSprite, useLoadTexture, useUpdate } from 'nacatamalon';
import * as Settings from '../settings';

export const Player = () => {
    const texture = useLoadTexture({
        src: '/example-assets/assets/gammix.png',
        key: 'gammix'
    });

    const player = createSprite({
        texture,
        transform: {
            x: 160,
            y: 190,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    let vx = Settings.VELOCITY.x;
    let vy = Settings.VELOCITY.y;

    useUpdate((delta) => {
        player.transform.x += vx * delta;
        player.transform.y += vy * delta;

        if (player.transform.x < 32 || player.transform.x > 448) {
            vx *= -1;
        }

        if (player.transform.y < 60 || player.transform.y > 288) {
            vy *= -1;
        }
    });
};
```

`scenes/game.ts`:

```ts
import { createScene, useScene, useSpawn } from 'nacatamalon';
import { Player } from '../components/player';

export const Game = () => {
    useScene().launch('Hud');
    useSpawn(Player)();

    return createScene();
};
```

`scenes/hud.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { HealthBar } from '../components/healthbar';

export const Hud = () => {
    useSpawn(HealthBar)();

    return createScene();
};
```

`settings.ts`:

```ts
import { getColor } from 'nacatamalon';

export const WIDTH = 480;
export const HEIGHT = 320;
export const HEALTH = getColor('#4ade80');
export const EMPTY = {
    r: 1,
    g: 1,
    b: 1,
    a: 0.15
};
export const VELOCITY = {
    x: 150,
    y: 110
};
```
