Sounds
The Sounds
class is responsible for managing and controlling sound effects within a game or application. It uses SoundPool
objects to load, play, and manage multiple sound effects, providing methods to retrieve, play, and pause sounds.
Constructor
new Sounds();
Creates a new Sounds
instance that stores and manages sound effects.
- Returns:
A new
Sounds
object.
Properties
sounds
(Object
): A collection ofSoundPool
objects, where the keys are sound names and the values areSoundPool
objects that handle playback and control of each sound.
Methods
load(soundDefinitions)
load(soundDefinitions);
Loads multiple sound effects into the Sounds
object based on the provided array of sound definitions. The sounds are stored in the sounds
property for later reference.
-
Parameters:
soundDefinitions
(Array
): An array of sound definitions, where each object contains:name
(String
): The name/key used to reference the sound.path
(String
): The path to the sound file.size
(Number
): The size of the sound pool (the number of instances of the sound).volume
(Number
): The volume of the sound, usually between 0 and 1.loop
(Boolean
): Whether the sound should loop.
-
Returns:
void
-
Example:
const sounds = new Sounds();sounds.load([{name: 'jump',path: './sounds/jump.mp3',size: 5,volume: 0.5,loop: false,},{name: 'explosion',path: './sounds/explosion.mp3',size: 3,volume: 1.0,loop: false,},]);
get(name)
get(name);
Retrieves a SoundPool
object by its name. This method allows access to any sound that was loaded using the load()
method.
-
Parameters:
name
(String
): The name of the sound to retrieve.
-
Returns: A
SoundPool
object corresponding to the specified sound name. If the sound has not been loaded, it returnsundefined
. -
Example:
const jumpSound = sounds.get('jump');if (jumpSound) {console.log('Jump sound loaded:', jumpSound);}
play(name)
play(name);
Plays the specified sound by calling the play()
method on the associated SoundPool
object.
-
Parameters:
name
(String
): The name of the sound to play.
-
Returns:
void
-
Example:
// Play the jump soundsounds.play('jump');
pause(name)
pause(name);
Pauses the specified sound by calling the pause()
method on the associated SoundPool
object.
-
Parameters:
name
(String
): The name of the sound to pause.
-
Returns:
void
-
Example:
// Pause the jump soundsounds.pause('jump');
Example Usage
// Create a Sounds instanceconst sounds = new Sounds();
// Load sound definitions from an arraysounds.load([ { name: 'jump', path: './sounds/jump.mp3', size: 5, volume: 0.5, loop: false, }, { name: 'explosion', path: './sounds/explosion.mp3', size: 3, volume: 1.0, loop: false, },]);
// Play the jump soundsounds.play('jump');
// Pause the jump soundsounds.pause('jump');
Notes
- The
load()
method uses theSoundPool
class to manage sound playback, ensuring that multiple instances of the same sound can be played simultaneously if needed (depending on thesize
parameter). - Ensure that sounds are loaded before trying to play or pause them using the
play()
orpause()
methods. - The
SoundPool
class handles individual sound controls like volume and looping.