เสียงเกม
- หน้าก่อน รูปภาพเกม
- หน้าต่อไป มวลที่มีความเร็วแก่งเกม
กรุณาปรับเสียงขึ้นต่ำสูงขึ้น. คุณได้ฟังเสียงชนกับประกายหรือไม่เมื่อสี่เหลี่ยมสีแดงชนกับอุปกรณ์ปฏิบัติการ?
วิธีเพิ่มเสียง?
ใช้ HTML5 <audio> ที่มีอยู่เพื่อเพิ่มเสียงและเพลงเข้าไปในเกมของคุณ:
ในตัวอย่างด้านล่าง พวกเราสร้างตัวปฏิบัติการสร้างสรรค์ใหม่เพื่อจัดการ sound:
ตัวอย่าง
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 ใหม่ โปรดใช้ เสียง
คำแปลงที่สร้างสรรค์ ขณะที่กล่องสีแดงชนกับตัวปฏิบัติการบรรทัดที่มีอยู่ จะเล่นเสียง:
ตัวอย่าง
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 ใหม่ และเริ่มเล่นเมื่อเริ่มเกม:
ตัวอย่าง
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(); }
- หน้าก่อน รูปภาพเกม
- หน้าต่อไป มวลที่มีความเร็วแก่งเกม