Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm using this function to play sounds when things collide into each other. You can see an example of it at <a href="http://www.jellyrobotics.com/2013/01/16/box-2d/" rel="nofollow">http://www.jellyrobotics.com/2013/01/16/box-2d/</a>. The overlapMax variable is to allow for multiple iterations of the sound file to be played at once. In other words, you can start playing it again before the previous instance finishes.</p> <pre><code>var JellySoundInstace = 0; var JellySound = function( audiofile ) { // should be optional variable because not all objects have the ability // to play multiple times and possibly overlap. For example, the explosion sound // from a single tank blowing up will only play one time for that tank instance // var overlapMax = 3; // PRIVATE instance variables here // var tracks = new Array(); var soundID = "jellysound" + JellySoundInstace++; var track = 0; this.load = function( audiofile ) { var i; for ( i=0; i&lt;overlapMax; i++ ) { var object = null; if ( ieVersion(8) ) { object = document.createElement('div'); var iesound = ''; iesound = iesound + '&lt;object id="'+soundID+'track'+i+'" type="audio/x-wav" data="'+audiofile+'" width="200" height="16"&gt;'; iesound = iesound + '&lt;param name="src" value="'+audiofile+'" /&gt;'; iesound = iesound + '&lt;param name="volume" value="2" /&gt;'; iesound = iesound + '&lt;param name="autoplay" value="false" /&gt;'; iesound = iesound + '&lt;param name="autostart" value="0" /&gt;'; iesound = iesound + '&lt;param name="pluginurl" value="http://www.apple.com/quicktime/download/" /&gt;'; iesound = iesound + '&lt;/object&gt;'; object.id = soundID+'track'+i+'div'; object.innerHTML = iesound; object.style.visibility = 'hidden'; object.style.position = 'absolute'; object.style.left = '0px'; object.style.top = '0px'; } else { object = document.createElement('audio'); object.setAttribute('id',soundID+'track'+i); object.setAttribute('src',audiofile); } document.body.appendChild( object ); var newsound = document.getElementById(soundID+'track'+i); // needs to be handled with a method &amp; params // newsound.volume = 0.02; tracks.push( newsound ); } } this.play = function() { if ( tracks.length==0 ) return; if ( ieVersion(8) ) { tracks[track].Play(); track++; track%=tracks.length; return; } tracks[track].play(); track++; track%=tracks.length; } this.load( audiofile ); return this; } function ieVersion( iecheck ) { if ( !(/MSIE (\d+\.\d+);/.test(navigator.userAgent)) ) return 0; var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number return (ieversion &lt;= iecheck); } // Sample Usage // var collisionSound = new JellySound( "dink.wav" ); collisionSound.play(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload