Pausing to listen for the silence before beginning the next melody

Greetings! I have successfully developed an app using Phaser and now I am eager to create a sequence comprising of two distinct sounds:

  1. "You have" (saved as audioSound[0])
  2. "Finished" (also saved as audioSound[1])

My goal is to be able to repeatedly use the first sound while incorporating various words or sentences for the second one. I am struggling with getting these sounds to play in succession rather than simultaneously.

Currently, my code looks like this:

audioSound[0].play();

audioSound[1].play();

othercode...

This setup plays both sounds at the same time. Any suggestions on how to ensure that the sounds play one after the other, and only proceed to execute the othercode... once the second sound has completed playing?

Thank you in advance!

EDIT: following @Julian's response Hello - for clarification, here is the code I am using to queue items and play them. While the queued items indeed play in sequence, the subsequent function/code executes concurrently with the sound rather than waiting for the sound(s) to finish.

function playSequence(soundArray) {
    soundArray[0].play();
    soundArray.forEach(function(element, index, array) {
        if (soundArray[index + 1]) {
            soundArray[index].onStop.addOnce(function() {
                soundArray[index + 1].play();
                }, this);
            }
        });

Answer №1

Give this a shot:

function preload() {

    game.load.audio('explosion-sound', 'explosion.wav');
    game.load.audio('lose-sound', 'lose.mp3');

}

var loseSound;
var explosionSound;

function create() {
    //Load the sounds
    explosionSound = game.add.audio('explosion-sound');
    loseSound = game.add.audio('lose-sound');

    //Set up actions after each sound finishes playing
    explosionSound.onStop.addOnce(function() { loseSound.play(); }, this);
    loseSound.onStop.addOnce(function() { console.log('Greetings!'); }, this)

    //Start with the explosion sound
    explosionSound.play();

    ...
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Setting a locale for date labels in Chart.js: A step-by-step guide

I have a Vue component that generates various charts using Chart.js. Everything is working fine, except for one issue - I can't seem to set a locale for the date labels on the X-axis. I attempted to set the moment locale at the beginning of the scrip ...

Is there a way to create optional sections within a reusable component's template in a Vue 3 application?

Recently, I've been immersed in developing a Single Page Application (SPA) using the powerful combination of Vue 3, TypeScript, and integrating The Movie Database (TMDB) API. One interesting challenge I encountered was with my versatile movie card co ...

Transforming DOM elements into Objects

Here are some values that I have: <input name="Document[0][category]" value="12" type="text"> <input name="Document[0][filename]" value="abca.png" type="text" > I am looking for a way to convert them into an object using JavaScript or jQuer ...

Is there a way to create a discord.js bot that can search for past messages without the need for a json file or storing them in a database?

Similar to the search feature in Discord. Imagine being able to enter !search [user] [query] and getting a response like "50 messages match your query." This would be like a word counting bot that doesn't need a database or local storage.The bot ...

The inclusion of jquery.ui results in a 400 bad request error

I currently have my HTML structured like this <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <script src="http://code.jquery.com/jquery-1.10.2.js"></script> <script src="http://code.jquery.com/ui/1.1 ...

How can I prevent right-clicking with Ctrl+LeftMouseClick in Firefox on MacOS?

I'm looking to implement a shortcut using Ctrl+LeftMouseClick in my React project. It functions perfectly on Chrome on my Mac, but in Firefox the shortcut initiates a right mouse click (event.button = 2). I believe this may be due to MacOS's Rig ...

I am trying to launch an Android application on a device using Appium and Desired capabilities, but I am facing difficulty in navigating to elements that are not currently visible

When using Appium and Desired Capability to launch an Android application on a device, I encountered difficulty navigating to elements that are not in the screen view of the same page. Attempting to use JavaScriptExecutor proved to be incompatible with A ...

What is an effective method for coordinating JQuery animations simultaneously?

My current understanding of $("#someElement").animate() is that it will run asynchronously in relation to other JavaScript statements. For example: $("#anotherElement").css("display", "none"); $("#someElement").animate(); //The CSS display may change a ...

Building a navigation system with previous and next buttons by implementing JavaScript

I am currently working with AngularJS and I have data that is being received in the form of an array containing two objects. As a newcomer, I am still trying to figure this out. data[ { "something":"something1", "something":"something1", "something":"some ...

To compare two JSON files that contain identical values but different keys in order to generate a consolidated table

My goal is to create a comprehensive table by merging data from two different JSON files. One file contains names, work positions, and ages, while the other file includes emails, names, and job roles. The challenge lies in the fact that they use different ...

Error in Express application due to uncaught SyntaxError

I am attempting to live stream data using websockets to Chartjs, however I keep encountering the following error. main.js:1 Uncaught SyntaxError: Unexpected token <https://i.sstatic.net/9OCI1.png https://i.sstatic.net/bmGeW.gif What could be causi ...

Accessing data in JSON format from a URL

I'm working on a website that analyzes data from the game Overwatch. There's this link () that, when visited, displays text in JSON format. Is there a way to use JavaScript to read this data and display it nicely within a <p> tag on my si ...

Converting a string into a Date in Typescript while disregarding the timezone

Upon receiving a date in string format like this (e.g.): "11/10/2015 10:00:00" It's important to note that this is in UTC time. However, when creating a Date object from this string, it defaults to local time: let time = "11/10/2015 10:00:00"; let ...

Deactivate a radio button group based on the selection of a previous group

When submitting a pay change request for an employee in the HR Department, there are 2 sets of radio buttons to consider. The 2nd group should only be enabled if "Yes" is selected in the first group. However, if the user switches from "Yes" to something in ...

Adjusting background size using jQuery as the window is resized

Exploring ways to dynamically resize the background image of my website based on the current window dimensions using jQuery. At the moment, I have tried the following approach: $(window).resize(function() { $("body").css({"background ...

What is the process of combining JS functions with PHP echo in code?

I am currently working on creating a popout menu for an array of values that are being displayed from the database. The goal is to show the corresponding popout menu when the svg is clicked, but I am running into an issue where it only works for the first ...

Tips for creating an Anchor Tag hyperlink that clears the previous page history

Whenever I click on the anchor tag for the first time (deep blue link), it takes me to a dynamic graph that displays a curve being rendered. However, on clicking the anchor tag for the second time (pale pink color), it still redirects me to the same dynam ...

How to properly extend Array and Proxy in ES6?

Currently, I am attempting to create a custom implementation of an Array/Object (which would end up being quite similar, I suppose), and I have run into a problem that is really frustrating me. As you can see, 'b' is only an instance of an Array ...

jQuery toggle functioning in one scenario, but failing in another

My experience with JS/Jquery is limited which might explain why I'm struggling with this. I am attempting to hide some text on a page and then make it visible again by clicking on a toggle link. The JavaScript code below is what I have been using. The ...

Managing the pressure of numerous ajax requests using RxJS

Looking for a solution using RxJS 5: Imagine I am retrieving a list of categories from a REST API. For each category, I need to fetch subcategories from another endpoint. Then, for each subcategory, I need to retrieve products and their detailed descrip ...