[kml_flashembed movie="/labs/swf/volumeExperiment.swf" height="150" width="450" /]

I had a good friend ask me if I knew of a resource for a volume controler, and I didn't know of any tutorial or code of the top of my head so I went ahead and wrote it for AS2 so I hope this can help some others out there.

[as]import mx.utils.Delegate;

//Set Variables var mySnd :Sound; var levelSpeed :Number; var musicOn :Boolean;

//SetUp Items

mySnd = new Sound(); mySnd.attachSound("soundLoop"); levelSpeed = 5; volLevel_txt.text = 100; volSpeed_txt.text = levelSpeed; musicOn = false;

//Buttons volumeOn_mc.onRelease = Delegate.create ( this, volOn); volumeOff_mc.onRelease = Delegate.create ( this, volOff); control_mc.onRelease = Delegate.create ( this, controls);

//Functioins ** DANGER :) function volOn() : Void {

var volLev = getVolume();

onEnterFrame = function() {

var volLev = getVolume(); var levelSpeed = getSpeed();

if (volLev < 100 ) {

setVolume( volLev + levelSpeed); }else{

setVolume(100); delete onEnterFrame; trace("sound on"); } }

};

function volOff() : Void {

onEnterFrame = function() {

var volLev = getVolume(); var levelSpeed = getSpeed();

if (volLev > 0 ) {

setVolume( volLev - levelSpeed); }else{

setVolume(0); delete onEnterFrame; trace("sound off"); } } };

function getSpeed():Number { return (volSpeed_txt.text > 100 ? (100,setSpeed(100)) : Number(volSpeed_txt.text) ); }

function setSpeed(val) { volSpeed_txt.text = val

} function getVolume():Number {

return mySnd.getVolume(); }

function setVolume(val) :Void {

mySnd.setVolume(val); volLevel_txt.text = val; }

function controls() {

if (musicOn == false) { control_mc.gotoAndStop("on"); mySnd.start(0,999); musicOn = true; }else { control_mc.gotoAndStop("off"); mySnd.stop(); musicOn = false; } } [/as]