Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 触发声音点击_Javascript_Html_Css - Fatal编程技术网

Javascript 触发声音点击

Javascript 触发声音点击,javascript,html,css,Javascript,Html,Css,我一直在尝试在我创建的游戏中添加类似火箭的声音,但我不确定如何在onClick回调中触发声音,所以每次我按下向船射击时,它都会播放 这是我的密码: console.log(JSON.stringify(gameBoard)); // set event listener for all elements in gameboard, run fireTorpedo function when square is clicked gameBoardContainer.addEventListener

我一直在尝试在我创建的游戏中添加类似火箭的声音,但我不确定如何在
onClick
回调中触发声音,所以每次我按下向船射击时,它都会播放

这是我的密码:

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++;
        }
    }
}

}

您喜欢这个解决方案吗…:欢迎来到堆栈溢出!你的密码在哪里?你试过什么?如果有人向你寻求帮助并仅仅发布了这些信息,你能帮助他们吗?请阅读。尝试上传您的代码以帮助您,如果我们没有看到任何无法帮助您的内容。请:)