Book Image

Babylon.js Essentials

By : Julien Moreau-Mathis
Book Image

Babylon.js Essentials

By: Julien Moreau-Mathis

Overview of this book

Are you familiar with HTML5? Do you want to build exciting games and Web applications? Then explore the exciting world of game and Web development with one of the best frameworks out there: Babylon.JS. Starting from the beginning, the book introduces the required basics for 3D development and the knowledge you need to use the Babylon.js framework. It focuses on the simplicity provided by Babylon.js and uses a combination of theory and practice. All the chapters are provided with example files ready to run; each example file provides the previously learned features of the framework. Finally, developers will be ready to easily understand new features added to the framework in the future.
Table of Contents (15 chapters)
Babylon.js Essentials
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

Playing 2D sounds


The Babylon.js framework provides an audio engine based on WebAudio. It allows you to easily add 2D and 3D sounds using the provided tools that are developed for you by the Babylon.js team.

Creating 2D sounds

The Babylon.js framework provides a BABYLON.Sound class. This class allows you to create and manage 2D and 3D sounds for your scene. To add a sound, the only thing you need to do is to create a new BABYLON.Sound object, as follows:

var sound = new BABYLON.Sound("sound_name", "sound_file", scene); 

You can now get access to methods such as .play.pause, and .stop.

In fact, the sounds are loaded asynchronously so that you cannot call sound.play() right after creating the new sound object. This is why the BABYLON.Sound constructor provides a readyToPlayCallback parameter after the scene in order to handle the loading process. To play the sound when loaded, simply set the readyToPlayCallback parameter, as follows:

var sound = new BABYLON.Sound("sound_name", "sound_file...