Play Sound

Play a sound effect

Description

'play sound <sound_id> [with [properties]]'

The play sound action let's you, as it name says, play sound effects in your game. You can play as many sound effects as you want simultaneously.

To stop the sound, check out the Stop Sound documentation.

Action ID: Sound

Reversible: Yes

Requires User Interaction: No

Parameters

Properties

The following is a comprehensive list of the properties available for you to modify certain behaviors of this action.

Assets Declarations

To play a sound, you must first add the file to your assets/sound/ directory and then declare it. To do so, Monogatari has an has a function that will let you declare all kinds of assets for your game.

Monogatari.assets ('sound', {
    '<sound_id>': 'soundFileName'
});

Supported Formats

Each browser has it's own format compatibility. MP3 however is the format supported by every browser.

If you wish to use other formats, you can check a compatibility table to discover what browsers will be able to play it.

Examples

Play Sound

The following will play the sound, and once the sound ends, it will simply stop.

Monogatari.script ({
    'Start': [
        'play sound riverFlow'
        'end'
    ]
});

Loop Sound

The following will play the sound, and once the sound ends, it will start over on an infinite loop until it is stopped using the Stop Sound Action.

Monogatari.script ({
    'Start': [
        'play sound riverFlow with loop'
        'end'
    ]
});

Fade In effect

The following will play the sound, and will use a fade in effect.

Monogatari.script ({
    'Start': [
        'play sound riverFlow with fade 3'
        'end'
    ]
});

Custom Volume

The following will set the volume of this sound to 73%.

Monogatari.script ({
    'Start': [
        'play sound riverFlow with volume 73'
        'end'
    ]
});

Please note however, that the user's preferences regarding volumes are always respected, which means that this percentage is taken from the current player preferences, meaning that if the player has set the volume to 50%, the actual volume value for the sound will be the result of:

All Together

Of course, you can combine all of this properties, and remember the order doesn't really matter, you can write the properties on the order that feels more natural to you.

Monogatari.script ({
    'Start': [
        'play sound riverFlow with volume 100 loop fade 20'
        'end'
    ]
});

Last updated