1
\$\begingroup\$

I'm trying to play background music for my game when it starts but I can't get it to work right. I'm not sure what's the right logic to do it. I this my code (written in TypeScript):

backgroundAudio() {
        this.countAudio++;
        if (this.countAudio == 200) {
            let audio = new Audio();
            audio.src = "audio/background.mp3";
            audio.load();
            audio.play();
            this.countAudio = 0;
        }

}

Basically this method it's called over and over again in the loop of the game but when the game starts it has a short delay until the count gets to 200. I'm not sure how I should be doing it if I want to start when the game starts but have a delay until the song is played and than get called again.

\$\endgroup\$
  • \$\begingroup\$ Could you provide some information about what language you are using, and what libraries to handle sound? \$\endgroup\$ – TomTsagk Jun 1 '18 at 10:58
  • \$\begingroup\$ I'm using typescript and to play the sound it's just an object from javascript i think \$\endgroup\$ – Player Jun 1 '18 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.