Engine.js supports two layers of audio: per-object sound effects tied to individual figures and driven by game events, and a global background track that plays continuously while the scene is running. Both use the browser’s nativeDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/OmarMtya/engine.js/llms.txt
Use this file to discover all available pages before exploring further.
Audio API under the hood, so no external libraries are required.
Per-object sounds with Sonido
A Sonido component is attached to a figure’s transform and defines which audio clip to play and under what condition. Create one with new $g.Sonido({ src, activacion }) and assign it to figura.transform.sonido.
Constructor parameters
An
HTMLAudioElement (new Audio()) with its src property set to the URL of the audio file. Use URL.createObjectURL(file) to load from a user-selected file, or pass a plain URL string.The event that triggers playback. One of three values:
'inicial'— the sound plays once as soon as the scene starts (when Play is pressed).'colision'— the sound plays each time this figure begins overlapping another rigid body.'colisionInversa'— the sound plays each time this figure stops overlapping another rigid body.
Attaching a sound to a figure
Activation modes explained
activacion value | When it fires |
|---|---|
'inicial' | Once on scene start (Play pressed) |
'colision' | Every time the figure starts overlapping a rigid body |
'colisionInversa' | Every time the figure stops overlapping a rigid body |
Changing activation type at runtime
You can switch the activation mode on an existing sound at any time by updating theactivacion property directly:
Removing a sound
Setfigura.transform.sonido to null to detach the sound from the figure entirely:
Global background audio
In addition to per-object sounds, you can load a single background track that plays automatically whenever the scene is running and pauses when the scene is paused. In the editor this is controlled by the volume icon in the toolbar. In code, hold a reference to anAudio object and play or pause it in sync with $g.Animar() and $g.DetenerAnimacion():
bgAudio.play() and pressing Pause calls bgAudio.pause().
Sound reference
| Concept | API | Notes |
|---|---|---|
| Per-object sound | figura.transform.sonido = new $g.Sonido({ src, activacion }) | Tied to a specific figure |
| Activation — scene start | activacion: 'inicial' | No rigido required |
| Activation — collision begin | activacion: 'colision' | Requires rigido |
| Activation — collision end | activacion: 'colisionInversa' | Requires rigido |
| Remove a sound | figura.transform.sonido = null | Detaches cleanly |
| Global background | bgAudio.play() / bgAudio.pause() | Independent of figures |