Flash Sound and Volume part 3
Flash Add Volume up and Volume Down
Draw something for an Volume Up button and a Volume Down Button
my buttons look like this
call the Volume up button 'btnUp' and the Volume Down button btnDown. Don't forget to name the instance.
<syntaxhighlight lang="csharp" line="1" > btnUp.onRelease = function() {
//Set the volume equal to the current volume + 5 or 100 whichever is less (don't let the volume go higher than 100) mySound.setVolume( Math.min((mySound.getVolume() + 5) , 100));
}; </csharp>
<syntaxhighlight lang="csharp" line="1" > btnDown.onRelease = function() {
//Set the volume equal to the current volume -5 or 0 whichever is more (don't let the volume go lower than 0) mySound.setVolume( Math.max((mySound.getVolume() - 5), 0));
}; </csharp>
Code for key presses
<syntaxhighlight lang="csharp" line="1" >
//Do this on Every Frame
this.onEnterFrame = function() {
if(Key.isDown(Key.RIGHT)){ //Right Key is Pressed
//Play the sound 1000 times
mySound.start(0,1000);
}
if(Key.isDown(Key.LEFT)){ //Left Key is Pressed //Play the sound 1000 times mySound.stop(); }
if(Key.isDown(Key.UP)){ //Down Key is Pressed
//Set the volume equal to the current volume + 5 or 100 whichever is less (don't let the volume go higher than 100)
mySound.setVolume( Math.min((mySound.getVolume() + 5) , 100));
}
if(Key.isDown(Key.DOWN)){ //Down Key is Pressed
//Set the volume equal to the current volume -5 or 0 whichever is more (don't let the volume go lower than 0)
mySound.setVolume( Math.max((mySound.getVolume() - 5), 0));
} }; </csharp>
Code to Change Sounds <syntaxhighlight lang="csharp" line="1" >
soundOne.onRelease = function() { mySound.stop(); mySound.attachSound("song_one"); mySound.start(0,1000); }
soundTwo.onRelease = function() { mySound.stop(); mySound.attachSound("song_two"); mySound.start(0,1000); }
</csharp>
Complete Code for Multi Song Player <syntaxhighlight lang="csharp" line="1" >
mySound = new Sound(); //New sound object called mySound mySound.attachSound("song_one"); //set mySound = to the named instance in the library btnPlay.onRelease = function() {
//Play the sound 1000 times mySound.start(0,1000);
}; btnStop.onRelease = function() {
//Stop playing the sound mySound.stop();
}; btnUp.onRelease = function() {
//Set the volume equal to the current volume + 5 or 100 whichever is less (don't let the volume go higher than 100) mySound.setVolume( Math.min((mySound.getVolume() + 5) , 100));
}; btnDown.onRelease = function() {
//Set the volume equal to the current volume -5 or 0 whichever is more (don't let the volume go lower than 0) mySound.setVolume( Math.max((mySound.getVolume() - 5), 0));
};
song_one_btn.onRelease = function() { mySound.stop(); mySound.attachSound("song_one"); mySound.start(0,1000); }
song_two_btn.onRelease = function() { mySound.stop(); mySound.attachSound("song_two"); mySound.start(0,1000); }
</csharp>
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