Hi.
Here is a sample of how you can achieve this.
If you are going to use OOP, try to let things the more generic, modular, and dynamic as possible.
In this demo, I created a class for a generic slider that has the only purpose of returning a number from 0 to 1. I called this returned value ratio.
The Audio class has methods to play and stop sounds and also to change their volume. The methods are static which means you don't need to create an instance of the Audio class to use them. You only have to write Audio.play(...), for example.
Also, you can separate your sounds in different channels by assigning an id to them. In my case, i used "bgm" and "sfx".
In the FLA, I added click events to the buttons to play a BGM and a SFX that live in the Library.
The slider class can inform the ratio value everytime it changes through a callback function called update that is a property of the callBacks object. Just call the Audio.volume method inside of the update function using the returned ratio value as an argument.
It's hard to explain OOP concepts. I hope it all makes some sense.
Please let me know if you have any questions.
AS3 code:
FLA
import flash.events.MouseEvent;
var bgmIsPlaying:Boolean = false;
function toggleBGM(e:MouseEvent):void
{
if (bgmIsPlaying)
Audio.stop("bgm");
else
Audio.play(new ActiveBGM(), "bgm", bgmSlider.ratio);
bgmIsPlaying = !bgmIsPlaying;
}
function playSFX(e:MouseEvent):void
{
Audio.play(new LaserSFX(), "sfx", sfxSlider.ratio);
}
bgmButton.addEventListener(MouseEvent.CLICK, toggleBGM);
sfxButton.addEventListener(MouseEvent.CLICK, playSFX);
bgmSlider.callBacks.update = function(ratio):void
{
Audio.volume("bgm", ratio);
};
sfxSlider.callBacks.update = function(ratio):void
{
Audio.volume("sfx", ratio);
};
Audio.as
package
{
import flash.media.SoundChannel;
import flash.media.SoundTransform;
public class Audio
{
public static var channels:Object = {};
public static function play(sound:*, channel:String, volume:Number = 1):void
{
var soundTransform:SoundTransform = new SoundTransform();
if (!channels[channel])
channels[channel] = new SoundChannel();
channels[channel] = sound.play();
soundTransform.volume = volume;
channels[channel].soundTransform = soundTransform;
}
public static function stop(channel:String):void
{
if (channels[channel])
channels[channel].stop();
}
public static function volume(channel:String, volume:Number):void
{
if (channels[channel])
{
var soundTransform:SoundTransform = new SoundTransform();
soundTransform.volume = volume;
channels[channel].soundTransform = soundTransform;
}
}
}
}
Slider.as
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
public class Slider extends Sprite
{
public var ratio:Number;
public var callBacks:Object = {};
public function Slider()
{
if (stage)
start(null);
else
addEventListener(Event.ADDED_TO_STAGE, start);
}
public function start(e:Event):void
{
ratio = getRatio();
removeEventListener(Event.ADDED_TO_STAGE, start);
addEventListener(MouseEvent.MOUSE_DOWN, _startMovingButton);
}
public function getRatio():Number
{
return button.x / (rec.width - button.width);
}
private function _startMovingButton(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, _moveButton);
stage.addEventListener(MouseEvent.MOUSE_UP, _stopMovingButton);
}
private function _moveButton(e:MouseEvent):void
{
button.x = mouseX;
if (button.x < 0)
button.x = 0;
else if (button.x > rec.width - button.width)
button.x = rec.width - button.width;
ratio = getRatio();
if (callBacks.update)
callBacks.update(ratio);
}
private function _stopMovingButton(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, _moveButton);
stage.removeEventListener(MouseEvent.MOUSE_UP, _stopMovingButton);
}
}
}
FLA download:
animate_cc_as3_volume_slider.zip - Google Drive
Regards,
JC