Reputation: 307
I'm making a flash payer. I already finished the volume part, and I also want to add two functions. One is when user click "add button", it will increase the current volume and change the volume display color. Another is when user click "minus button", it will decrease the current volume and change the volume display color.
How do I write these two function? I've been tried for loop but I'm so new to program so it's totally mess up...
My current actionscript code is
import flash.net.URLRequest;
import flash.media.Sound;
import flash.events.Event;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.media.SoundTransform;
var audiofile:URLRequest=new URLRequest("mvl_player_test.mp3");
var song:Sound=new Sound();
var songcontro:SoundChannel;
var resumetime:Number = 0;
song.addEventListener(Event.COMPLETE, songLoaded);
song.addEventListener(ProgressEvent.PROGRESS, trackLoad);
song.load(audiofile);
function songLoaded(evt:Event):void{//play songs
play_btn.addEventListener(MouseEvent.CLICK, clickedplay);
pause_btn.addEventListener(MouseEvent.CLICK, clickedpause);
volume_1.addEventListener(MouseEvent.CLICK, clickedVol_1);
volume_2.addEventListener(MouseEvent.CLICK, clickedVol_2);
volume_3.addEventListener(MouseEvent.CLICK, clickedVol_3);
volume_4.addEventListener(MouseEvent.CLICK, clickedVol_4);
volume_5.addEventListener(MouseEvent.CLICK, clickedVol_5);
volume_6.addEventListener(MouseEvent.CLICK, clickedVol_6);
volume_7.addEventListener(MouseEvent.CLICK, clickedVol_7);
}
function clickedplay(evt:MouseEvent):void{
play_btn.visible = false;
pause_btn.visible = true;
songcontro=song.play(resumetime);
addEventListener(Event.ENTER_FRAME, trackPlayback);
}
function clickedpause(evt:MouseEvent):void{
play_btn.visible = true;
pause_btn.visible = false;
resumetime = songcontro.position;
songcontro.stop();
removeEventListener(Event.ENTER_FRAME, trackPlayback);
}
function trackLoad(evt:ProgressEvent):void
{
load_progress.scaleX = evt.bytesLoaded / evt.bytesTotal;
}
function trackPlayback(evt:Event):void
{
var perPlayed:Number = songcontro.position / song.length;
played_progress.scaleX = perPlayed * (song_row.width) + song_row.scaleX;
}
Here is the code refer to volume display area
function clickedVol_1(evt:MouseEvent):void
{
var volControl:SoundTransform = songcontro.soundTransform;
volControl.volume = 0.1;
songcontro.soundTransform = volControl;
volume_1.visible = false;
volume_1_2.visible = true;
volume_2_2.visible = false;
volume_3_2.visible = false;
volume_4_2.visible = false;
volume_5_2.visible = false;
volume_6_2.visible = false;
volume_7_2.visible = false;
}
function clickedVol_2(evt:MouseEvent):void
{
var volControl:SoundTransform = songcontro.soundTransform;
volControl.volume = 0.3;
songcontro.soundTransform = volControl;
volume_1.visible = false;
volume_2.visible = false;
volume_1_2.visible = true;
volume_2_2.visible = true;
volume_3_2.visible = false;
volume_4_2.visible = false;
volume_5_2.visible = false;
volume_6_2.visible = false;
volume_7_2.visible = false;
}
function clickedVol_3(evt:MouseEvent):void
{
var volControl:SoundTransform = songcontro.soundTransform;
volControl.volume = 0.4;
songcontro.soundTransform = volControl;
volume_1.visible = false;
volume_2.visible = false;
volume_3.visible = false;
volume_1_2.visible = true;
volume_2_2.visible = true;
volume_3_2.visible = true;
volume_4_2.visible = false;
volume_5_2.visible = false;
volume_6_2.visible = false;
volume_7_2.visible = false;
}
function clickedVol_4(evt:MouseEvent):void
{
var volControl:SoundTransform = songcontro.soundTransform;
volControl.volume = 0.5;
songcontro.soundTransform = volControl;
volume_1.visible = false;
volume_2.visible = false;
volume_3.visible = false;
volume_4.visible = false;
volume_1_2.visible = true;
volume_2_2.visible = true;
volume_3_2.visible = true;
volume_4_2.visible = true;
volume_5_2.visible = false;
volume_6_2.visible = false;
volume_7_2.visible = false;
}
function clickedVol_5(evt:MouseEvent):void
{
var volControl:SoundTransform = songcontro.soundTransform;
volControl.volume = 0.6;
songcontro.soundTransform = volControl;
volume_1.visible = false;
volume_2.visible = false;
volume_3.visible = false;
volume_4.visible = false;
volume_5.visible = false;
volume_1_2.visible = true;
volume_2_2.visible = true;
volume_3_2.visible = true;
volume_4_2.visible = true;
volume_5_2.visible = true;
volume_6_2.visible = false;
volume_7_2.visible = false;
}
function clickedVol_6(evt:MouseEvent):void
{
var volControl:SoundTransform = songcontro.soundTransform;
volControl.volume = 0.7;
songcontro.soundTransform = volControl;
volume_1.visible = false;
volume_2.visible = false;
volume_3.visible = false;
volume_4.visible = false;
volume_5.visible = false;
volume_6.visible = false;
volume_1_2.visible = true;
volume_2_2.visible = true;
volume_3_2.visible = true;
volume_4_2.visible = true;
volume_5_2.visible = true;
volume_6_2.visible = true;
volume_7_2.visible = false;
}
function clickedVol_7(evt:MouseEvent):void
{
var volControl:SoundTransform = songcontro.soundTransform;
volControl.volume = 1;
songcontro.soundTransform = volControl;
volume_1.visible = false;
volume_2.visible = false;
volume_3.visible = false;
volume_4.visible = false;
volume_5.visible = false;
volume_6.visible = false;
volume_7.visible = false;
volume_1_2.visible = true;
volume_2_2.visible = true;
volume_3_2.visible = true;
volume_4_2.visible = true;
volume_5_2.visible = true;
volume_6_2.visible = true;
volume_7_2.visible = true;
}
Thanks for any answer
Upvotes: 0
Views: 683
Reputation: 3907
I think your problem here is the fact that you have several volume buttons. I would have one bar and two buttons, plus and minus. When you click the plusButton
, you run a function setVolume(volume)
that sets the new volume to a variable, making sure it is between 0 and 1. At the end of the function you update the bar graphics depending on the volume percentage. I dont like the idéa that you have that many buttons. Make one button with multiple frames. Each frame lights up a new volume button. When you click the whole clip you calculate the percentage of the mousePositionX
and the with of the volumeBar
to get the new volume. In the updateBArGraphics function you just compare the percentage to the amount of frames you have and use goToAndStop(volumeFrame)
.
This is written out of my head, so there will be errors:
private function setVolume(newVolume : Number) : void
{
if(newVolume > 1)
newVolume = 1;
if(newVolume < 0)
newVolume = 0;
volControl.volume = newVolume;
songcontrol.soundTransform = volControl;
updateVolumeBarGraphics();
}
private function onVolumeBarClickHandler(event : MouseEvent) : void
{
// calculate where on the bar you clicked using MouseX and bar.width
var calculatedPercentage : Number =
// set new volume
setVolume(caculatedPercentage);
}
private function onVolumeMinusClickHandler(event : MouseEvent) : void
{
setVolume(volControl.volume - 0.2);
}
private function onVolumeMinusClickHandler(event : MouseEvent) : void
{
setVolume(volControl.volume + 0.2);
}
private function updateVolumeBarGraphics(newVolume : Number) : void
{
var volumeFrame : int = Math.floor(newVolume * volumeBar.totalFrames) + 1;
volumeBar.gotoAndStop(volumeFrame);
}
Something like that.
Upvotes: 1