Sounds
The sound
property in templates
associates a sound effect with a game element. You can use different types of sounds, ranging from predefined templates to custom sounds or URLs from PFXR.
Creating a Sound Effect with a Predefined Template
The createSound
method accepts two parameters:
- The type of sound, which can be one of the following:
DEFAULT
RANDOM
PICKUP
LASER
EXPLOSION
POWERUP
HIT
JUMP
BLIP
FALL
FART
- An optional parameter: the seed.
The seed is a number used to initialize the sound generator in a random but deterministic way. This means that if you generate a sound with a certain type (e.g., PICKUP
) and provide a seed, the same sound will be produced every time you reuse that seed. If no seed is provided, a different random sound will be generated each time.
Example of generating a predictable sound using a seed:
If you do not pass a seed, each generation will produce a different sound.
Creating a Sound from a PFXR URL
You can also create a sound from a URL generated through the PFXR interface. The URL contains the parameters to generate a specific sound.
Example of assigning a sound generated via a PFXR URL to a template:
Creating a Custom Sound with a Sound
Object
For advanced customization, you can create a Sound
object and use it as the value for the sound
property in a template. The object can contain parameters such as waveForm
, frequency
, sustainTime
, and decayTime
.
You can find the complete documentation of the available parameters for the Sound
object in the official PFXR repository.
Example of assigning a custom sound to a template:
Playing a Sound Dynamically
To play a sound independently of the templates
, you can use the game
object returned by the createGame
function. The game.playSound
method allows you to play a dynamic sound.
Example:
Full Example
Here is an example showing how to assign sounds to different templates and how to play them dynamically: