Example 5. Audio

We can play a sound using the play method. Whenever we click on the greenish screen, a sound should be head.


This is the javascript file. We use this.sound as the variable name so its scope is this file.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// ex05. Audio

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'ex05',
               { preload: preload, create: create});

function preload() {
    game.load.audio('ex05Sound', 'sounds/ex05.mp3');
}

function create() {
    // Set backgound color
    game.stage.backgroundColor = "#0FFF0F";

    // sound
    this.sound = game.add.audio("ex05Sound");
    
    // call playSound if click detected
    game.input.onDown.add(playSound, this);
}

function playSound() {
    // Play sound
    this.sound.play()
}

No comments:

Post a Comment