Be a Supporter!

As: Volume Slider Tutorial

  • 3,702 Views
  • 11 Replies
New Topic Respond to this Topic
StarCleaver
StarCleaver
  • Member since: Jan. 3, 2003
  • Offline.
Forum Stats
Member
Level 29
Blank Slate
As: Volume Slider Tutorial Jul. 4th, 2005 @ 04:47 PM Reply

AS: Main

OVERVIEW

This tutorial will use the concepts found in AS: Sound It will cover how to utilize the Sound() object to make a play/pause button and a volume control.

First you need to pick out your song and import it to the library. Find the song in the library and right-click it. Click on linkage and then check the box that says "export for actionscript". You'll notice the "identifier" bar light's up. Put any name in there, something easy to type and remember. Just call it "mySong" to keep it easy.

Now, go to the first frame of the main timeline, which is where we're going to type our script. We need to tell Flash that we're making a new Sound() object. The way to do this is pretty simple:

theSong = new Sound();

Now, notice I put "theSong". That's how we're going to identify our Sound object from now on. Right now flash only knows that "theSong" is a sound object, but it doesn't know what song we want it to point to, so we have to tell it what song we want:

theSong.attachSound("mySong");

We just attached "mySong" (the one you imported to the library) to "theSong". So anytime we tell "theSong" to play, flash will play "mySong". Get it?

Now, we don't want 'theSong' to play right now at the beginning of the movie, so we will make a play button and a stop button for the song.

Make a button and give it the instance name of "pButton", without the quotes. Do the same for a stop button and call it "sButton", again, without the quotes.

Now, we're going back to the first frame of the main timeline to create some functions for our buttons. First we need to make our play button play the song.

pButton.onRelease=function(){
theSong.start(0,0);
}

We're telling the song to start at second number 0 of the song, and it will loop 0 times, therefore only playing once through. If you want it to play more than that, change the number. If you want it to loop forever you can add this little function:

theSong.onSoundComplete=function(){
theSong.start(0,0);
}

That just says that when the song is done, it will play again, therefore looping forever.

*NOTE: You can make a pause button very easily by making the play button a movie clip with two frames. One frame will be the play button, the second frame will be the pause button. Give both of them instance names and reference them inside the movie clip. Write a simple function like the one above for the pause button and have it define a variable as the number of seconds the sound has been playing. You cna do this by using the Sound() object method of "position"

Example: songPosition = theSong.position

This will define "songPosition" as the number of miliseconds the song has been playing. You can then use this as the starting point for the song when using theSong.star(songPosition, 0). This way, you can pause the song and then return the the exact spot.

If this needs more clarifying, just ask. I didn't go into grave detail because it is not the focus of this tutorial.

*END NOTE

Now, we need to make the stop button work.

sButton.onRelease=function(){
theSong.stop("mySong");
}

We're telling our sound object "theSong" to stop playing the song "mySong". Pretty basic.

Well, our song now works and plays and stops. Now we just need to adjust the volume.

Let's make a line. Make it straight up and down for simplicity's sake, and give it the instance name of "slideRail", again without the quotes.

Now make the actual slider that will slide up and down the slide raile. Give it the instance name of "slider" Now stick the slider on top of the slide rail, so it looks like a volume adjuster. It doesn't matter where it's at, just as long as it look decent.

Now, go back to the first frame of the main timeline, where we have all of our code. We need to tell the slider to let us drag it, but only so far. For this we will use "startDrag".

We're going to make this very dynamic so you can movie it anywhere without the code screwing up. To do this we need to define how high the slideRail is, and how long it is. We'll use getBounds() for simplicity. To use getBounds() we need to define our bouding variable. We'll call it slideRailBound

slideRailBound = slideRail.getBounds(_root);

The _root just tells us that we are doing this in respect to the main timeline's coordinates.

Now to actually make our slider draggable.

slider.onPress = function() {
slider.startDrag(false, slideRail._x, slideRailBound.yMin, slideRail._x, slideRailBound.yMax);
};

Notice we did onPress=function(){ } this time. We did that because we want the person to be able to release the slide button and have it stop dragging. startDrag(lock, left, top, right, bottom) We basically set where the slider can and cannot be dragged. We said that the slider can't be dragged beyond the top of teh slideRail or the bottom of the slideRail, and that we can't movie it off of the slideRail whatsoever.

To stop draggin the slider we say:

slider.onRelease = function() {
slider.stopDrag();
};
slider.onReleaseOutside = function() {
slider.stopDrag();
};

We say that if the user releases the mouse button whether the mouse is over the button or not, that the slider will stop dragging with the mouse.

Now to make the slider's position actually mean something.

_root.onEnterFrame = function() {
theSong.setVolume(slideRailBound.yMax-slider._y);
};

We're saying that Flash needs to continuously set the volume of the theSong to the position of the slider, relative to the slideRail. If you move the slider up, the song gets louder, if you movie it down, it gets softer. To set the initial volume of the song to 100, you can say:

_root.onLoad = function() {
theSong.setVolume(100);
slider._y = slideRailBound.yMin;
slider._x = slideRail._x;
};

We'll set the volume of theSong to 100 and also put the slider at the 100 position. We'll also stick the slider right on the slideRail so it doesn't look shitty. Basically, you can put the slider anywhere and it will cling to the slideRail from the start.

That's the end of AS: Volume Slider Tutorial. This entry was written for Flash MX at a previous date and was written to answer a question of a user in this forum. It is meant as a guide. This concepts written above can also be used to set the balance and position of a song.

Questions? Just Ask


I could surely die
If I only had some pie
Club-a-Club Club, son

BBS Signature
Nemo
Nemo
  • Member since: Jun. 13, 2003
  • Offline.
Forum Stats
Member
Level 34
Game Developer
Response to As: Volume Slider Tutorial Jul. 4th, 2005 @ 04:57 PM Reply

That's pretty cool. I was thinking of adding one of these to the Options menu of my game. This will help a lot!


BBS Signature
Hellraiser
Hellraiser
  • Member since: Jan. 4, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to As: Volume Slider Tutorial Nov. 25th, 2005 @ 02:55 PM Reply

ow do you work this with All Sounds. like, your making a volume foir the entire game.


BBS Signature
Hellraiser
Hellraiser
  • Member since: Jan. 4, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to As: Volume Slider Tutorial Nov. 25th, 2005 @ 02:56 PM Reply

At 11/25/05 02:55 PM, -Hellraiser- wrote: ow do you work this with All Sounds. like, your making a volume foir the entire game.

myMusic = new Sound();
myMusic.attachSound("Loop");
myMusic.start( 0, 1000 );

here is an example of what im using, loop is the first song im using, the rest are, loop1,2,,3,4,5,6, etc.


BBS Signature
Bankay
Bankay
  • Member since: Mar. 29, 2006
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to As: Volume Slider Tutorial Mar. 29th, 2006 @ 06:58 AM Reply

Nice tutorial u got here ... but that's not exactly what i'm looking for...tough i think i'm going to use this one ... do u got some more tutorials ?

Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Bankay
Bankay
  • Member since: Mar. 29, 2006
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to As: Volume Slider Tutorial Mar. 29th, 2006 @ 07:49 AM Reply

on topic : How can u make the "slider" to go on an oval line and still modifing the volume right ?

GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to As: Volume Slider Tutorial Mar. 29th, 2006 @ 07:50 AM Reply

Tween it.


BBS Signature
Bankay
Bankay
  • Member since: Mar. 29, 2006
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to As: Volume Slider Tutorial Mar. 29th, 2006 @ 08:26 AM Reply

lol , ok i tween it ...but it will just play endlesly ...from one state to another ...if u ment something else...reply

bizz3rk
bizz3rk
  • Member since: Jan. 28, 2006
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to As: Volume Slider Tutorial Mar. 29th, 2006 @ 08:55 AM Reply

thanks a bunch!!

Mogly
Mogly
  • Member since: Sep. 5, 2004
  • Offline.
Forum Stats
Member
Level 25
Animator
Response to As: Volume Slider Tutorial Mar. 29th, 2006 @ 11:49 AM Reply

is it possible to have the slider control the whole sound coming from the computer, same way paltalk does?


WEBSITE
BLOG ~ Dont fuck around with my dog. All that I can see I steal. ~
NG FFR ~ Automatic for the people.

BBS Signature
West-End-Pro
West-End-Pro
  • Member since: Feb. 15, 2006
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to As: Volume Slider Tutorial Aug. 10th, 2006 @ 09:18 AM Reply

But how would you do this in buttons? Something like:

on(press){
_root.theSong.setVolume ++;
}

?? HELP! ^^