//////////////////////////////////////// EnhancedSound 1.00 //////////////////////////////////////// // Tatsuo Kato Oct. 2002 //////////////////////////////////////////////////////////////////////////////////////////////////// /* EnhancedSound is a sub class of built-in Sound class, which extends Sound with some extra properties and methods. The properties and the methods of Sound are available as they are, with this EnhancedSound as well. Properties added (All optional) file defaultVolume (Used for the methods 'fadeOut' and 'fadeIn') dontOverLap (A boolean value which specifies whether the sound can be played overlapping itself when it's already playing. If true, it stops the previous play before it starts.) offset loopCount xPosPan (An x coordinate number value which sets its panning position. i.e. Assigning 0 to xPosPan sets the pan to leftmost and Stage.Width to rightmost.) Methods added setDefaultVolume setDontOverlap setOffset setLoopCount setXPosPan (Can be useful when you want the sound's pan to follow the mc's _x. mySound.setXPosPan(0) is equivalent to mySound.setPan(-100). mySound.setXPosPan(Stage.Width) is equivalent to mySound.setPan(100).) play (Equivalent to 'start' of Sound, except it supports 'dontOverlap' and switching 'file'.) fadeOut (Fades-out the sound during the time specified with the passed milliseconds value.) fadeIn (Fades-in the sound during the time specified with the passed milliseconds value. Optionally, you can specify the file to play at this time as well.) // Usage example 1 // "bell.wav" and "buzzer.wav" in the library of _level10 _level10.createEmptyMovieClip("SE_bell_mc", 1); _level10.createEmptyMovieClip("SE_buzzer_mc", 2); // The parameters, except the first one which specifies the timeline, are all optional. _global.SE_bell = new EnhancedSound(_level10.SE_bell_mc, "bell.wav"); _global.SE_buzzer = new EnhancedSound(_level10.SE_buzzer_mc, "buzzer.wav", 70, true, 0.008, 0, Stage.Width*0.7); SE_bell.play(); SE_buzzer.play(); // Usage example 2 // "song1.wav" and "song2.wav" in the library of _level1 _global.BGM1 = new EnhancedSound(_level1, null, 50, true, 0, 9999); BGM1.play("song1.wav"); // You may switch 'file' to another at a later time BGM1.play("song2.wav"); // Also you may do something like this to cross-fade two BGMs. // "song3.wav" in the library of _level2 _global.BGM2 = new EnhancedSound(_level2, "song3.wav", 50, true, 0, 9999); BGM1.fadeOut(3000); BGM2.fadeIn(3000); */ //The parameters except 'timeline' are all optional _global.EnhancedSound = function (timeline, file, defaultVolume, dontOverlap, offset, loopCount, xPosPan) { super(timeline); if (file == undefined) return; this.attachSound(file); if (defaultVolume == undefined) return; this.setDefaultVolume(defaultVolume); if (dontOverlap == undefined) return; this.setDontOverlap(dontOverlap); if (offset == undefined) return; this.setOffset(offset); if (loopCount == undefined) return; this.setLoopCount(loopCount); if (xPosPan == undefined) return; this.setxPosPan(xPosPan); } pt = EnhancedSound.prototype = new Sound(); pt.defaultVolume = 100; pt.setDefaultVolume = function (defaultVolume) { this.defaultVolume = defaultVolume; this.setVolume(defaultVolume); } pt.setDontOverlap = function (bool) { this.dontOverlap = bool; } pt.setOffset = function (offset) { this.offset = offset; } pt.setLoopCount = function (loopCount) { this.loopCount = loopCount; } pt.setXPosPan = function (xPosPan) { this.setPan(xPosPan / (Stage.width/2/100) - 100); } //Pass the file parameter only when you want to switch 'file' or specify 'file' if not specified yet. pt.play = function (file) { if (this.dontOverlap) { this.stop(); } if (file != undefined) { this.attachSound(file); } this.start(this.offset, this.loopCount); } pt.fadeOut = function (ms) { var tempVolume = this.defaultVolume; var step = tempVolume / (ms/80); var o = this; var id = setInterval( function () { if (tempVolume > 0) { o.setVolume(tempVolume -= step); } else { o.stop(); clearInterval(id); o.setVolume(o.defaultVolume); } }, 80 ); } //Pass the file parameter only when you want to switch 'file' or specify 'file' if not specified yet. pt.fadeIn = function (ms, file) { this.setVolume(0); this.play(file); var tempVolume = 0; var step = this.defaultVolume / (ms/80); var o = this; var id = setInterval( function () { if (tempVolume < o.defaultVolume) { o.setVolume(tempVolume += step); } else { clearInterval(id); o.setVolume(o.defaultVolume); } }, 80 ); } delete pt;