Flash Sound and Volume part 3

esse quam videri
Revision as of 18:29, 25 January 2016 by Jeff (talk | contribs) (Text replacement - "</csharp>" to "</syntaxhighlight>")
Jump to: navigation, search

Flash Add Volume up and Volume Down

Draw something for an Volume Up button and a Volume Down Button

my buttons look like this

FlashVol AllButtons.png

call the Volume up button 'btnUp' and the Volume Down button btnDown. Don't forget to name the instance.

1 btnUp.onRelease = function() {
2     //Set the volume equal to the current volume + 5 or 100 whichever is less (don't let the volume go higher than 100)
3     mySound.setVolume( Math.min((mySound.getVolume() + 5) , 100));
4 };
1 btnDown.onRelease = function() {
2     //Set the volume equal to the current volume  -5 or 0 whichever is more (don't let the volume go lower than 0)
3         mySound.setVolume( Math.max((mySound.getVolume() - 5), 0));
4 };


Code for key presses

 1 //Do this on Every Frame
 2 this.onEnterFrame = function() {
 3 	if(Key.isDown(Key.RIGHT)){ //Right Key is Pressed
 4 		//Play the sound 1000 times
 5 		mySound.start(0,1000); 
 6 	}
 7 	
 8 	if(Key.isDown(Key.LEFT)){ //Left Key is Pressed
 9 		//Play the sound 1000 times
10 		mySound.stop(); 
11 	}
12 	
13         if(Key.isDown(Key.UP)){ //Down Key is Pressed
14 	 	//Set the volume equal to the current volume + 5 or 100 whichever is less (don't let the volume go higher than 100)
15                 mySound.setVolume( Math.min((mySound.getVolume() + 5) , 100));
16 	}
17 	
18         if(Key.isDown(Key.DOWN)){ //Down Key is Pressed
19 		//Set the volume equal to the current volume  -5 or 0 whichever is more (don't let the volume go lower than 0)
20                 mySound.setVolume( Math.max((mySound.getVolume() - 5), 0));
21 	}
22 };

Code to Change Sounds

 1 soundOne.onRelease = function() {
 2 	    mySound.stop(); 
 3 		mySound.attachSound("song_one"); 
 4 		mySound.start(0,1000); 
 5 }
 6 
 7 soundTwo.onRelease = function() {
 8 	    mySound.stop(); 
 9 		mySound.attachSound("song_two"); 
10 		mySound.start(0,1000); 
11 }

Complete Code for Multi Song Player

 1 mySound = new Sound();                  //New sound object called mySound
 2 mySound.attachSound("song_one");    //set mySound = to the named instance in the library
 3 btnPlay.onRelease = function() {
 4     //Play the sound 1000 times
 5     mySound.start(0,1000); 
 6 };
 7 btnStop.onRelease = function() {
 8     //Stop playing the sound
 9     mySound.stop(); 
10 };
11 btnUp.onRelease = function() {
12     //Set the volume equal to the current volume + 5 or 100 whichever is less (don't let the volume go higher than 100)
13     mySound.setVolume( Math.min((mySound.getVolume() + 5) , 100));
14 };
15 btnDown.onRelease = function() {
16     //Set the volume equal to the current volume  -5 or 0 whichever is more (don't let the volume go lower than 0)
17         mySound.setVolume( Math.max((mySound.getVolume() - 5), 0));
18 };
19 
20 song_one_btn.onRelease = function() {
21 	    mySound.stop(); 
22 		mySound.attachSound("song_one"); 
23 		mySound.start(0,1000); 
24 }
25 
26 song_two_btn.onRelease = function() {
27 	    mySound.stop(); 
28 		mySound.attachSound("song_two"); 
29 		mySound.start(0,1000); 
30 }

Final Files

link to final swf

http://iam.colum.edu/si/FlashVolume/FlashVolume.swf - source files

http://iam.colum.edu/si/FlashVolumePause/FlashVolumePause.swf - source files