Game Sound
<!--
main_leaderboard, all: [728,90][970,90][320,50][468,60]-->
Game Sound
❮ Previous
Next ❯
Turn up the volume. Do you hear a "dunk" when the red square hits an obstacle?
var myGameArea;
var myGamePiece;
var myObstacles = ;
var mysound;
function startGame() {
myGameArea = new gamearea();
myGamePiece = new component(30, 30, "red", 10, 75);
mysound = new sound("bounce.mp3");
myGameArea.start();
}
function gamearea() {
this.canvas = document.createElement("canvas");
this.canvas.width = 320;
this.canvas.height = 180;
document.getElementById("canvascontainer").appendChild(this.canvas);
this.context = this.canvas.getContext("2d");
this.pause = false;
this.frameNo = 0;
this.start = function() {
this.interval = setInterval(updateGameArea, 20);
}
this.stop = function() {
this.pause = true;
clearInterval(this.interval);
}
this.clear = function(){
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
function component(width, height, color, x, y) {
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.update = function() {
ctx = myGameArea.context;
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
this.crashWith = function(otherobj) {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var otherleft = otherobj.x;
var otherright = otherobj.x + (otherobj.width);
var othertop = otherobj.y;
var otherbottom = otherobj.y + (otherobj.height);
var crash = true;
if ((mybottom otherbottom) || (myright otherright)) {
crash = false;
}
return crash;
}
}
function updateGameArea() {
var x, y, min, max, height, gap;
for (i = 0; i < myObstacles.length; i += 1) {
if (myGamePiece.crashWith(myObstacles[i])) {
mysound.play();
myGameArea.stop();
}
}
if (myGameArea.pause == false) {
myGameArea.clear();
myGameArea.frameNo += 1;
if (myGameArea.frameNo == 1 || everyinterval(150)) {
x = myGameArea.canvas.width;
y = myGameArea.canvas.height - 100;
min = 20;
max = 100;
height = Math.floor(Math.random()*(max-min+1)+min);
min = 50;
max = 100;
gap = Math.floor(Math.random()*(max-min+1)+min);
myObstacles.push(new component(10, height, "green", x, 0));
myObstacles.push(new component(10, x - height - gap, "green", x, height + gap));
}
for (i = 0; i < myObstacles.length; i += 1) {
myObstacles[i].x += -1;
myObstacles[i].update();
}
myGamePiece.x += myGamePiece.speedX;
myGamePiece.y += myGamePiece.speedY;
myGamePiece.update();
}
}
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();
}
}
function everyinterval(n) {
if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
return false;
}
function moveup(e) {
myGamePiece.speedY = -1;
}
function movedown() {
myGamePiece.speedY = 1;
}
function moveleft() {
myGamePiece.speedX = -1;
}
function moveright() {
myGamePiece.speedX = 1;
}
function clearmove(e) {
myGamePiece.speedX = 0;
myGamePiece.speedY = 0;
}
startGame();
How to Add Sounds?
Use the HTML5 <audio> element to add sound and music to your games.
In our examples, we create a new object constructor to handle sound objects:
Example
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();
}
}
<!--
mid_content, all: [300,250][336,280][728,90][970,250][970,90][320,50][468,60]-->
To create a new sound object use the sound
constructor, and when
the red square hits an obstacle, play the sound:
Example
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;
}
}
...
}
Try it Yourself »
Background Music
To add background music to your game, add a new sound object, and start
playing when you start the game:
Example
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();
}
Try it Yourself »
❮ Previous
Next ❯