I'm building a video player and am kinda stuck at the volume slider part. It's a YouTube style vertical slider, meaning if the slider is in the top position volume should be 100% and if the slider is dragged to the bottom position sound should be 0. Currently it's doing the opposite of what I want :(
Dragging the slider down will make the sound louder, while dragging up lowers it.
Here is my code below dealing with the volume slider.
// Sound Controller Settings ······························
soundController = new SoundController();
soundContrColor = soundController.colorChip;
soundContrGray = soundController.grayCover;
soundContrGray.visible = false;
soundController.visible = true;
soundController.buttonMode = true;
soundController.soundSlider.addEventListener(MouseEvent.MOUSE_DOWN, sliderDown);
// SoundController Button Mouse Events ························
public function sliderDown(event:MouseEvent):void
{
soundController.soundSlider.startDrag(false, dragBounds);
soundController.soundSlider.addEventListener开发者_高级运维(MouseEvent.MOUSE_MOVE, sliderMove);
soundController.soundSlider.addEventListener(MouseEvent.MOUSE_UP, sliderUp);
soundContrGray.visible = true;
}
public function sliderMove(event:MouseEvent):void
{
soundContrGray.height = soundController.soundSlider.y;
userVolume = Math.round(((soundContrGray.y * soundContrGray.height) / 10) - 4);
//userVolume = soundContrGray.height;
setVolume(userVolume);
trace("soundController.mouseY = "+soundController.soundSlider.y);
trace("soundContrColor.height = "+Math.round(soundContrGray.height));
trace("userVolume = "+userVolume+"\r");
event.updateAfterEvent();
}
public function sliderUp(event:MouseEvent):void
{
lastVolPoint = soundContrGray.height;
setVolume(userVolume);
event.updateAfterEvent();
soundController.soundSlider.stopDrag();
soundController.soundSlider.removeEventListener(MouseEvent.MOUSE_MOVE, sliderMove);
soundController.soundSlider.removeEventListener(MouseEvent.MOUSE_UP, sliderUp);
}
[TRACES] when I drag all the way to the top:
soundController.mouseY = 6
soundContrGray.height = 6
userVolume = 0
[TRACES] when I drag all the way down:
soundController.mouseY = 56
soundContrGray.height = 56
userVolume = 30
I believe this is where the problem lies:
userVolume = Math.round(((soundContrGray.y * soundContrGray.height) / 10) - 4);
The (-4) is an offset value so when you drag it all the way to turn it off, it's 0 and not 4.
I need to reverse this somehow, so the traces above will swap... going down will make userVolume = 4 and going up will make it 30.Thanks in advance for anyone that takes a look at this one! :)
I must be missing something because it can't be this simple, can it?
userVolume = 30-Math.round(((soundContrGray.y * soundContrGray.height) / 10) - 4);
Since you are getting 0 when you want 100% volume, and 30 when you want 0 volume... try this
setVolume(100 - (userVolume * 100 / 30));
Substitude 0 in equation:
setVolume(100 - (0 * 100 / 30)) --> simplifies to
setVolume(100);
Substitute 30 in equation:
setVolume(100 - (30 * 100 / 30)); --> simplifies to
setVolume(100 - (3000 / 30)); --> simplifies to
setVolume(100 - 100); --> simplifies to
setVolume(0);
Conceptually what you want to do is to have the sound lower the taller soundContrGray
. So you need a max value (or in this case a max height) to compare to current value to. I take it that the 30
is the volume slider background's height. I'll use bg
for the background height.
userVolume = (soundContrGray.height / bg.heigth);
userVolume = Math.round((1 - userVolume) * 100);
That'll give a relative volume setting independent of the height's of the actual elements, or where they are on the screen.
userVolume = Math.round(100 * (1 - soundContrGray.height /(soundContrGray.y - 4)))
精彩评论