# Music on/off button tut?

**URL:** <https://forum.kirupa.com/t/music-on-off-button-tut/6320>\
**Category:** flash\
**Created:** [October 4, 2002, 4:06am UTC](https://forum.kirupa.com/t/music-on-off-button-tut/6320 "2002-10-04T04:06:29Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![DyersEve](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@DyersEve](https://forum.kirupa.com/u/DyersEve)\
**Post date:** [October 4, 2002, 4:06am UTC](https://forum.kirupa.com/t/music-on-off-button-tut/6320/1 "2002-10-04T04:06:29Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 4, 2002, 7:57am UTC](https://forum.kirupa.com/t/music-on-off-button-tut/6320/2 "2002-10-04T07:57:01Z")

</div>

There actually is a tutorial on the site for this…

[http://www.kirupa.com/developer/actionscript/sound.asp](http://www.kirupa.com/developer/actionscript/sound.asp)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 4, 2002, 8:10pm UTC](https://forum.kirupa.com/t/music-on-off-button-tut/6320/3 "2002-10-04T20:10:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 4, 2002, 8:26pm UTC](https://forum.kirupa.com/t/music-on-off-button-tut/6320/4 "2002-10-04T20:26:29Z")

</div>

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

Add this on Frame 1

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

```

Create your start sound button and add these actions…

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

```

Create your sound off button and add these actions…

```auto
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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 4, 2002, 8:35pm UTC](https://forum.kirupa.com/t/music-on-off-button-tut/6320/5 "2002-10-04T20:35:24Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 4, 2002, 8:44pm UTC](https://forum.kirupa.com/t/music-on-off-button-tut/6320/6 "2002-10-04T20:44:37Z")

</div>

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…

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

```

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

```auto
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…

```auto
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:)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 4, 2002, 8:59pm UTC](https://forum.kirupa.com/t/music-on-off-button-tut/6320/7 "2002-10-04T20:59:15Z")

</div>

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

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

```

Music On Movie Clip

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

```

Music Off Movie Clip

```auto
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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 4, 2002, 9:02pm UTC](https://forum.kirupa.com/t/music-on-off-button-tut/6320/8 "2002-10-04T21:02:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 4, 2002, 9:05pm UTC](https://forum.kirupa.com/t/music-on-off-button-tut/6320/9 "2002-10-04T21:05:07Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 4, 2002, 9:08pm UTC](https://forum.kirupa.com/t/music-on-off-button-tut/6320/10 "2002-10-04T21:08:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 4, 2002, 9:15pm UTC](https://forum.kirupa.com/t/music-on-off-button-tut/6320/11 "2002-10-04T21:15:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 4, 2002, 9:20pm UTC](https://forum.kirupa.com/t/music-on-off-button-tut/6320/12 "2002-10-04T21:20:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 4, 2002, 9:26pm UTC](https://forum.kirupa.com/t/music-on-off-button-tut/6320/13 "2002-10-04T21:26:59Z")

</div>

Well, when you put it like that…

Your welcome=)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [October 5, 2002, 8:49pm UTC](https://forum.kirupa.com/t/music-on-off-button-tut/6320/14 "2002-10-05T20:49:01Z")

</div>

Hey hey, guess what thread I just found 🙂

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](http://www.kirupaforum.com/showthread.php?s=&threadid=7085)
