Aller au contenu

Fond sonore

Ismaël

Le son permet de donner une identité sonore au jeu et favoriser l’immersion du joueur.

  1. load.js
    const MP3 = [
    "tough zombeat",
    "wooosh",
    "off",
    ]
  2. component.js
    function music(config) {
    const param = {
    volume: 1.0,
    ...config
    }
    return {
    audio: null,
    add() {
    if (param.music) {
    this.audio = play(param.music, { loop: true, volume: param.volume })
    onSceneLeave(() => this.audio.stop())
    }
    }
    }
    }
  3. game.js
    scene("game", () => {
    const config = { ...LEVEL_CONFIG, ...LEVELS[CURRENT_LEVEL].config }
    const map = LEVELS[CURRENT_LEVEL].map.split("\n")
    const level = addLevel(map, config)
    add([
    music(config),
    multiplayerCamera(),
    ])
    setGravity(config.gravity)
    setBackground(config.backgroundColor)
    on('jump', 'player', () => play('wooosh'))
    on('drop', 'player', () => play('off'))
    on('respawn', 'player', (obj) => obj.play('idle'))
    on('sleep', 'player', (obj) => obj.play('sleep'))
    on('awake', 'player', (obj) => obj.play('idle'))
    on('jump', 'player', (obj) => obj.play('jump', { speed: 4, onEnd: () => obj.play('idle') }))
    on('drop', 'player', (obj) => obj.play('worry'))
    }
  4. game.js
    const LEVEL_CONFIG = {
    music: 'tough zombeat',
    volume: 1,
    tileWidth: 64,
    tileHeight: 64,
    backgroundColor: "afe1ff",
    gravity: 3200,
    tiles: {
    "#": () => [ // player 1
    sprite("bean"),
    platformerController(),
    alive(),
    opacity(),
    scale(),
    health(1, 4),
    area(),
    anchor("bot"),
    body(),
    respawn(),
    falling(),
    ],
    "=": () => [ // block
    sprite("grass"),
    area(),
    body({ isStatic: true }),
    anchor("bot"),
    offscreen({ hide: true }),
    ],
    },
    };