Music on/off button tut?

Could someone point me to a good tut on making the popular Music on/off buttons, thanks. (and if there is one on this site and im just stupid for not looking hard enough or something, sorry :slight_smile:

There actually is a tutorial on the site for this…

http://www.kirupa.com/developer/actionscript/sound.asp

Thanks that worked nicely, but what i want is the music to start from the beginning and then have a button to turn it off or back on again.

Ok, going by the code given to you in the tutorial…

Add this on Frame 1

	kirupaSound = new Sound(this);
	kirupaSound.attachSound("coolsound");
	kirupaSound.start(0, 99);

Create your start sound button and add these actions…

on (release) {
	kirupaSound = new Sound(this);
	kirupaSound.attachSound("coolsound");
	kirupaSound.start(0, 99);
}

Create your sound off button and add these actions…

on (release) {
	stopAllSounds();
}

Wow, that was my first ever music on and off button thing that I made:) I tried it out and it does work. The only problem is that if you keep pressing the on button, it will keep overlapping the loop. I don’t know how to disable buttons in Flash 5, but in Flash MX it is mybutton.enabled = false. Actually, I think that only works for movie clips that act as buttons. Hmmmm…

Thanks man, and now that im looking at your code there, im feeling pretty stupid for not thinking of that in the first place. Thanks a ton though :slight_smile:

No problem. I got mine to work Flawlessly in Flash MX now. I have no clue if it will work in Flash 5, but I will post my code as a JUST IN CASE…ya know…

I created 2 <B>movie clips</B>. One I gave the instance name of “buttonOn” and the other I gave the instance name of “buttonOff” (both without quotes).

On the frame add these actions…

kirupaSound = new Sound(this);
kirupaSound.attachSound("coolsound");
kirupaSound.start(0, 99);
_root.buttonOn.enabled = false;

Now on the “buttonOn” movie clip, add these actions…

on (release) {
	kirupaSound = new Sound(this);
	kirupaSound.attachSound("coolsound");
	kirupaSound.start(0, 99);
	_root.buttonOff.enabled = true;
	this.enabled = false;
}

Now on the “buttonOff” movie clip, add these actions…

on (release) {
	stopAllSounds();
	_root.buttonOn.enabled = true;
	this.enabled = false;
}

What these do is originally disables the music on button when the frame loads, but when you click the music off button, that button becomes disabled and only the music on button is enabled. Then when you click the music on button, the music on button becomes disabled so that only the music off button is enabled. Works great:) If it doesn’t work in Flash 5, hopefully you can find a way so that it will work:)

Wait wait wait… I optimized a bit more, this time I used prototypes. Which I heard do work in Flash 5. So in this case, even if enabling doesn’t work, you might be able use the prototype way I use here…

Frame

MovieClip.prototype.createSound = function() {
	startSound = new Sound(this);
	startSound.attachSound("coolsound");
	startSound.start(0, 99);
	_root.buttonOn.enabled = false;
};
createSound();

Music On Movie Clip

on (release) {
	this.createSound();
	_root.buttonOff.enabled = true;
	this.enabled = false;
}

Music Off Movie Clip

on (release) {
	stopAllSounds();
	_root.buttonOn.enabled = true;
	this.enabled = false;
}

Tah dah, I think I actually optimized something. That is a rarity for me:P

Yeah…tried it, but it will only allow onMouseEvents for buttons, not movie clips. :frowning:

oh ok:( Sorry

You should at least try the prototypes or a function to create the function for the sound so you don’t have it pasted twice. This makes for some nice optimization:) , but if not, we know the copy and paste twice method works too :evil:

Hehe, aight i’ll keep pickin away at it.

Good Luck:) Sorry I couldn’t help more.

Dont worry about it, before you came in i was scratching my head trying to figure out the basic actions to start/stop the music…so you were plenty help.

Well, when you put it like that…

Your welcome=)

Hey hey, guess what thread I just found :slight_smile:

It is a thread that explains how to disable buttons in Flash 5. And there is another workaround…ah, just read it, really good stuff that might be able to help you out…

http://www.kirupaforum.com/showthread.php?s=&threadid=7085