# Multiple scenes

> Three scenes registered when the game is created: only the first starts, and you change between them by name.

Example `multi-scene-demo`, running at https://nacatamalon.com/en/examples/multi-scene-demo. The whole catalogue: https://nacatamalon.com/llms/examples.md.

Three scenes (red, blue and green) registered when the game is created. Only the first one starts; the others wait until you change to them. Press **A**, **B** or **C** to jump to each.

Each change is `useScene().change(name)`: it starts the target scene and replaces the current one. It is asked for by **name** and not by function, which is what lets a scene arrive from anywhere, a chunk loaded later or a document read from disk, without whoever calls `change` having to hold it.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { WIDTH, HEIGHT, ROOMS } from './settings';
import { createRoom } from './scenes/room';

const Red = createRoom(ROOMS[0]);
const Blue = createRoom(ROOMS[1]);
const Green = createRoom(ROOMS[2]);

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

// Declare Scenes
game({
    Red,
    Blue,
    Green
}, 'Red');
```

`scenes/room.ts`:

```ts
import { createScene, createSprite, createText, getColor, useKeyboard, useLoadBitmapFont, useScene, useUpdate } from 'nacatamalon';
import { WIDTH, HEIGHT, ARCADE, WHITE, ROOMS } from '../settings';

export const createRoom = (self: typeof ROOMS[number]) => () => {
    const scene = useScene();
    const keyboard = useKeyboard();
    const font = useLoadBitmapFont(ARCADE);

    // A flat background is a sprite, not a mesh: there is no depth in the plane, and a
    // full-screen quad through a perspective camera is the long way round.
    createSprite({
        tint: getColor(self.colour),
        width: WIDTH,
        height: HEIGHT,
        anchor: {
            x: 0,
            y: 0
        },
        transform: {
            x: 0,
            y: 0
        },
    });

    const others = ROOMS.filter((other) => other !== self);

    createText({
        text: self.name.toUpperCase() + ' SCENE',
        font,
        style: {
            fontSize: 16
        },
        tint: WHITE,
        transform: {
            x: 40,
            y: 120
        }
    });
    createText({
        text: others.map((other) => other.key.toUpperCase() + ' = ' + other.name.toUpperCase()).join('   '),
        font,
        style: {
            fontSize: 8
        },
        tint: WHITE,
        transform: {
            x: 40,
            y: 200
        },
    });

    // A room never listens for its own key, so holding the one you arrived with does not
    // re-trigger the change once you are here.
    useUpdate(() => {
        for (const other of others) {
            if (keyboard.isDown(other.key)) {
                scene.change(other.name);
            }
        }
    });

    return createScene();
};
```

`settings.ts`:

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

export const WIDTH = 480;
export const HEIGHT = 320;
export const ARCADE = {
    json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
    atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png'
};
export const WHITE = getColor('#ffffff');
// This example changes scenes by their registered names, without importing the next scene.
export const ROOMS = [
    {
        name: 'Red',
        colour: '#ff4444',
        key: 'a'
    },
    {
        name: 'Blue',
        colour: '#4444ff',
        key: 'b'
    },
    {
        name: 'Green',
        colour: '#44ff44',
        key: 'c'
    },
];
```
