View Single Post
Codemonkey's Avatar Codemonkey Codemonkey is offline Super Moderator 2006-11-05 #18 Old  
015b - Creating enhanced sounds
Last edited by Codemonkey : 2006-11-11 at 02:18.
 
To make the sound objects more manageble, you can embed it in a container that 'adds' functionality to a sound object. For example, you can simplify playing, status checks, looping etc. by adding functions for that to the sound's container.

Here's an example:

ActionScript Code:
  1. function loadSound(sound:String, vol:Number, loop:Boolean):Object {
  2.     var d:Number = _root.getNextHighestDepth();
  3.     var emptyMC:MovieClip = _root.createEmptyMovieClip("empty" + d, d);
  4.  
  5. // multifunctional container
  6.     var SoundObj:Object = new Object();
  7.     SoundObj.sound = new Sound(emptyMC);
  8.     SoundObj.sound.attachSound(sound);
  9.     SoundObj.sound.setVolume(vol);
  10.     SoundObj.sound.onSoundComplete = function() {
  11.         SoundObj.isPlaying = false;
  12.     };
  13.  
  14.     SoundObj.isLoop = loop;
  15.     SoundObj.isPlaying = false;
  16.  
  17. // only start playing when sound is not already playing
  18. SoundObj.softStart = function(Play:Boolean) {
  19.     if (Play) {
  20.         if (!this.isPlaying) this.sound.start(0, this.isLoop ? 999 : 0);
  21.     } else {
  22.         if (this.isPlaying) this.sound.stop();
  23.     }
  24.  
  25.     this.isPlaying = Play;
  26. }
  27.  
  28. // restart playing no matter what
  29. SoundObj.hardStart = function(Play:Boolean) {
  30.     this.sound.stop();
  31.     this.isPlaying = false;
  32.     this.softStart(Play);
  33. }
  34.  
  35.     return SoundObj;
  36. }
Ofcourse, you can add only so much until it becomes more logical to create a SoundClass instead and hide the details away in there.