0

I have a simple word guessing game and spacebar is required to start. I wanted to play music along with this without any HTML but, no luck. Anybody able to help with this? I'm not getting any DOM errors either.

// press spacebar to start game
function startGame() {

    document.body.onkeyup = function(e){

        if(e.keyCode == 32){

            start();
            checkInput();
            document.getElementById('startBanner').style.display = 'none';

            let audio = new Audio('assets/audio/final_fight_selection.mp3')
            audio.play();

        }
    }
}
kyledvs
  • 3
  • 1
  • I think this question of your is just like this one over here, https://stackoverflow.com/questions/21935490/javascript-keypress-to-control-animation – HAMISI SALUM May 13 '20 at 19:50
  • I have my function working, the 'startBanner' line works as well as my two other functions. The main thing i can't get it to do is play the new audio variable. – kyledvs May 13 '20 at 19:55

1 Answers1

0

Do you have an audio tag? Think you might need to set the src as in: https://stackoverflow.com/a/21492698/4873576

Edit:

  document.body.onkeyup = function(e){
    if(e.keyCode == 32){
        const audio = document.createElement('audio');
        audio.src = ('https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3')
        audio.play();

    }
}

http://jsfiddle.net/n1zxdco2/1/