Skip to main content

Documentation 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.

$g.Sonido gives each game object its own sound effect, independently of the optional global background audio. A Sonido instance wraps a native HTMLAudioElement and an activacion string that tells the engine when to call .play() on that element. The instance is stored on figura.transform.sonido and the engine checks it on every relevant event during the animation loop.

Constructor

new $g.Sonido({ src, activacion })
src
HTMLAudioElement | null
The Audio object that the engine will play. Pass null to reserve the slot before a file has been loaded — the engine will skip playback silently until a real Audio object is assigned. Create one with new Audio() and set its src property to a URL or an object URL from a File.
activacion
string
required
The trigger mode that determines when the sound plays. Accepted values:
  • 'inicial' — plays once when $g.Animar() starts
  • 'colision' — plays each time this figure collides with another rigid body
  • 'colisionInversa' — plays when this figure stops colliding (separation event)

Activation Modes

ValueWhen it fires
'inicial'Once, at the moment $g.Animar() begins the loop
'colision'Each time the figure enters a collision with another rigid-body figure
'colisionInversa'Each time the figure exits (separates from) a collision

Examples

Attaching a collision sound from a file input

fileInput.onchange = function (event) {
    const audio = new Audio();
    audio.src = URL.createObjectURL(event.target.files[0]);
    audio.title = event.target.files[0].name; // used by the editor UI

    figura.transform.sonido = new $g.Sonido({
        src: audio,
        activacion: 'colision'
    });
};

Playing a sound at scene start

const audio = new Audio();
audio.src = 'path/to/start-jingle.mp3';

figura.transform.sonido = new $g.Sonido({
    src: audio,
    activacion: 'inicial'
});

$g.Animar(); // sound plays immediately as the loop begins

Playing a sound on separation

const audio = new Audio();
audio.src = 'path/to/land.mp3';

figura.transform.sonido = new $g.Sonido({
    src: audio,
    activacion: 'colisionInversa'
});

Reserving the slot before a file is chosen

The editor creates the Sonido instance when the checkbox is ticked, before the user picks an audio file:
figura.transform.sonido = new $g.Sonido({
    src: null,
    activacion: 'colision'
});

// Later, when the user picks a file:
const audio = new Audio();
audio.src = URL.createObjectURL(selectedFile);
figura.transform.sonido.src = audio;

Changing the trigger mode at runtime

figura.transform.sonido.activacion = 'colisionInversa';

Removing the sound

figura.transform.sonido = null;
The 'colision' and 'colisionInversa' trigger modes require figura.rigido to be set on the same figure. Without a Rigido component, collision events are never generated and the sound will not play regardless of the activacion value.
The src.title property is used by the built-in editor to display the audio file name in the attributes panel. If you are building a custom editor UI or want to label sounds for debugging, set this property after creating the Audio object:
audio.title = 'jump.mp3';
figura.transform.sonido = new $g.Sonido({ src: audio, activacion: 'colision' });

Build docs developers (and LLMs) love