0

I've been trying to add like a rocket sound on my game that I created, but I am not sure how to trigger a sound in the onClick callback, so it plays every time I press to shoot at a boat.

Here is my code:

console.log(JSON.stringify(gameBoard));
// set event listener for all elements in gameboard, run fireTorpedo function when square is clicked
gameBoardContainer.addEventListener('click', fireTorpedo, false);

// initial code via http://www.kirupa.com/html5/handling_events_for_many_elements.htm:
function fireTorpedo(e) {
    // if item clicked (e.target) is not the parent element on which the event listener was set (e.currentTarget)

    if (e.target !== e.currentTarget) {
        // extract row and column # from the HTML element's id
        const row = e.target.id.substring(1, 2);
        const col = e.target.id.substring(2, 3);
        //alert("Clicked on row " + row + ", col " + col);

        // if player clicks a square with no ship, change the color and change square's value
        if (gameBoard[row][col] === 0) {
            e.target.style.background = '#bbb';
            // set this square's value to 3 to indicate that they fired and missed
            gameBoard[row][col] = 7;
            totalClicks++;
            addClick();
            console.log(totalClicks);

        // if player clicks a square with a ship, change the color and change square's value
        } else if (gameBoard[row][col] === 1 ||
            gameBoard[row][col] === 2 || gameBoard[row][col] === 3 || gameBoard[row][col] === 4 || gameBoard[row][col] === 5) {
            e.target.style.background = 'red';
            // set this square's value to 2 to indicate the ship has been hit
            gameBoard[row][col] = 8;

            // increment hitCount each time a ship is hit
            hitCount++;
            totalClicks++;
        }
    }
}

}

Max Yankov
  • 12,551
  • 12
  • 67
  • 135
  • do like this solution ..... : https://stackoverflow.com/a/18826567/12781348 – Saeed Gholamzadeh Feb 06 '20 at 19:44
  • 2
    Welcome to Stack Overflow! Where is your code? What have you tried? If someone came to you for help and posted just this information, would you be able to help them? Please read [how to create a minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – disinfor Feb 06 '20 at 19:44
  • Try to upload your code to help you, if we don't see anything we can't help you. Please :) – ASASCED Feb 06 '20 at 19:49

0 Answers0