Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/OmarMtya/enginejs-module/llms.txt

Use this file to discover all available pages before exploring further.

Sonido attaches an HTMLAudioElement to a Transform and declares the condition under which it should play. Engine.js evaluates the activacion mode at two distinct points: at construction time for 'inicial', and inside Figura.tocandoRigidos() every frame for 'colision' and 'colisionInversa'. This makes it straightforward to add ambient loops, landing sounds, or airborne sounds to any Figura that has a Rigido component.

Constructor

new Sonido({ src, activacion })
src
HTMLAudioElement
required
The audio element to control. Construct it with new Audio('./path/to/file.mp3') and pass the element — not a plain string path. The engine calls .play() and .pause() on this reference directly.
activacion
string
required
Determines when the sound plays. Must be one of 'inicial', 'colision', or 'colisionInversa'. See the table below for details.

activacion Modes

ValueTriggerBehavior
'inicial'Scene startsrc.play() is called immediately inside the Sonido constructor. The sound should be treated as a looping ambient track; pause it manually or via DetenerAnimacion().
'colision'Collision start/endPlays when rigido.colision becomes true (figure lands); pauses when rigido.colision becomes false (figure leaves surface).
'colisionInversa'No collisionPlays when rigido.colision is false (figure is airborne); pauses when rigido.colision is true (figure is resting).

Instance Properties

src
HTMLAudioElement
The audio element passed at construction time.
activacion
string
The activation mode string: 'inicial', 'colision', or 'colisionInversa'.

Usage

const landingSound = new Audio('./land.mp3');

const box = new $g.Figura({
  tipo: 'cuadrado',
  transform: new $g.Transform({
    x: 100, y: 50,
    anchura: 50, altura: 50,
    relleno: '#4F46E5',
    sonido: new $g.Sonido({ src: landingSound, activacion: 'colision' })
  }),
  rigido: new $g.Rigido()
});
activacion: 'inicial' calls src.play() directly inside the Sonido constructor. Browsers block autoplay until the user has interacted with the page. Ensure that Sonido is constructed after a user gesture (click, keydown, etc.) or the play call will be rejected and the console will show an AbortError.
Sound triggering for 'colision' and 'colisionInversa' only occurs inside Figura.tocandoRigidos(). The figure must have a Rigido component for these modes to fire — a figure without rigido never calls tocandoRigidos(), so the sound will never play or pause automatically.
  • Figura — calls tocandoRigidos() which triggers sound
  • Transform — holds the Sonido reference
  • Rigidocolision flag drives 'colision' and 'colisionInversa' modes

Build docs developers (and LLMs) love