Canvas ການສະແດງສຽງ
ຍັງບັນຫາສຽງຂື້ນຫຼາຍຫນັງ. ຖ້າຮູບບາງວັດນະຄະດີສະແດງມາພາຍໃນວັດສະດຸນັກຢູ່ບ່ອນຫນັງຈະທີ່ເຈົ້າຟັງສຽງຕຳກັນຫນັງບໍ່?
ບາງວິທີການສະເໜີສຽງ?
ກວດກາຄວາມຈະເອົາສຽງແລະສຽງການສະແດງຫຼັງສອງໃນເກມຂອງທ່ານ ທີ່ໃຊ້ HTML5 <audio> element.
ໃນການຕັ້ງຕັ້ງຫຼັງກ່າວນີ້ ພວກເຮົາສ້າງພາກສ້າງຕັ້ງເປັນສັນຍາວ່າວ່າຈະດຳເນີນການຂັດຂວາງສຽງobject:
ອາການ
function sound(src) { this.sound = document.createElement("audio"); this.sound.src = src; this.sound.setAttribute("preload", "auto"); this.sound.setAttribute("controls", "none"); this.sound.style.display = "none"; document.body.appendChild(this.sound); this.play = function(){ this.sound.play(); } this.stop = function(){ this.sound.pause(); } }
ສຳລັບທີ່ຈະສ້າງ sound object ອີກໜຶ່ງອັນ ກະຕຸ້ມອີກ: sound
ການສ້າງພາກສ້າງຕັ້ງເປັນສັນຍາວ່າວ່າຫົວໜ້າສີແດງພົບກັບບັນດາບັນດາອຸປະກອນຂັດຂວາງແລ້ວສະແດງສຽງ:
ອາການ
var myGamePiece; var myObstacles = []; var mySound; function startGame() { myGamePiece = new component(30, 30, "red", 10, 120); mySound = new sound("bounce.mp3"); myGameArea.start(); } function updateGameArea() { var x, height, gap, minHeight, maxHeight, minGap, maxGap; for (i = 0; i < myObstacles.length; i += 1) { if (myGamePiece.crashWith(myObstacles[i])) { mySound.play(); myGameArea.stop(); return; } } ... }
ສຽງການສະແດງຫຼັງສອງ
ເພື່ອສະເໜີສຽງການສະແດງຫຼັງສອງໃນເກມຂອງທ່ານ ກະຕຸ້ມອອກສະເພາະອີກ sound object ແລະເລີ່ມສະແດງໃນເວລາທີ່ເລີ່ມເກມ:
ອາການ
var myGamePiece; var myObstacles = []; var mySound; var myMusic; function startGame() { myGamePiece = new component(30, 30, "red", 10, 120); mySound = new sound("bounce.mp3"); myMusic = new sound("gametheme.mp3"); myMusic.play(); myGameArea.start(); }