Learn Create Your Own Split Screen

Next Page

Adding sound to the game becomes really easy.

First we load the sounds we're going to use:

var scream = new Audio("http://www.s2js.com/snd/etc/scream.mp3"); var ooof = new Audio("http://www.s2js.com/snd/etc/ooof.mp3");
We'll play the "ooof" sound every time the hero bumps into an obstacle.
if (is_touching_an_obstacle(hero)) { hero.x = prev_x; hero.y = prev_y; ooof.play(); }
And we'll play the scream every time something bad happens to a fruit.
function make_bad_thing_happen_to_fruit (berry) { if (berry.costume < 5) { scream.play(); fruit_remaining--; berry.animation_continuous = false; berry.animation_final_costume = 9; berry.costume = 5; berry.animation_rate = 8; } }