Simple Mute button tutorial(s?)

7 posts

Flag Post

Well, i wanted to start this thread because i hate game with music or sounds with no mute button. If everyone want to put his own techniques in here, just to help (that’s the programming forum), here is mine:

This will create a rollover/rollout/disappear effect on the button so that you don’t always have a big mute button on your screen, it only appears when u rollover it.

Here is how i do it (some crap AS code :p):
put this code on your frame where the button is

var son_bouclier:Sound = new Sound();
son_bouclier.attachSound("bouclier");

_root.son_on = 1;

this.btn_son._alpha = 30;

if (_root.son_on == 1) {
	_root.son_bouclier.setVolume(0);
} else {
	_root.son_bouclier.setVolume(30);
}

//BOUTON SON ON/OFF
this.btn_son.onRelease = function() {

	if (_root.son_on == 1) {
		_root.son_bouclier.setVolume(0);
		this.gotoAndStop(2);
		_root.son_on = 0;
	} else {
		_root.son_bouclier.setVolume(30);
		this.gotoAndStop(1);
		_root.son_on = 1;
	}
};

this.btn_son.onRollOver = function() 
{
	this.onEnterFrame = function() {
		if (this._alpha <= 100) {
			this._alpha += 5;
		}
	};
};

this.btn_son.onRollOut = function() 
{
	this.onEnterFrame = function() {
		if (this._alpha >= 30) {
			this._alpha -= 5;

		}
	};
};

Then create a movie clip named btn_son on your scene. In this MC, two images with a stop(); on each. First is for the Sound ON graphics and second is for sound OFF graphics.

Import a sound in your library and go right click>Properties and set the link identifier to “bouclier”. Well that’s some french things, you can change it of course, but don’t forget to search and replace on the AS code.
I’m sure you can do it with a new empty MC like luksy explained on the other topic but well, i didn’t have energy to try ;)
Experiment !

If other devs want to post a better technique, i’m sure we can finish this thread with the best mute tutorial ever ^^

pixtiz


Edited 03/14/07 by arcaneCoder. Reason: Added code formatting for readability ;)

 
Flag Post

We’re actually going to do a standard control bar that will include a mute button on Kong pretty soon, too.

 
Flag Post


I would take a different approach to the button and just import something from my library, maybe just a nice little button with a rollover built into it… running two onEnterFrame handlers (and not deleting them) seems rather inefficient while a game is playing =)

So… First we begin with the music, using a .mp3 imported from the library. In this example, we assume an .mp3 file is in the library, with a linkage ID of “myMP3”


this.createEmptyMovieClip(“musicContainer”, this.getNextHighestDepth());
var music:Sound = new Sound(musicContainer);
music.attachSound(“myMP3”);
music.start();


This code also assumes you made a little button (MovieClip class, of course =P) and have the linkage ID set to “myButton”.


var musicOn:Boolean = true;
var playHeadVal:Number = 0;

var mcButton:MovieClip = attachMovie("myButton","mcButton",this.getNextHighestDepth(),{_x:0, _y:0, _visible:true});

mcButton.onPress = function():Void {

	if(musicOn) {
	
		playHeadVal = music.position;
		music.stop();
		musicOn = false;
	
	} else {
	
		music.start(playHeadVal * 1000);
		musicOn = true;
	}

}


Now you have some dynamic code that will import the sound and the button from your library, and you can make sure that the music doesn’t continuously play in the background while your game is muted. Essentially, I guess that makes this a “stop” button, but it could easily be converted to simply mute (using Sound.setVolume()) just like Pixtiz did above.


Edited 03/14/07 by arcaneCoder

 
Flag Post

The best way is to create a class for your sound objects that encapsulate all the functionality, and then another manager class to control all the sounds. I did this myself awhile back and created a sound manager class + component that handles all my loading and sound control automatically. It makes it easy to handle all sound functionality, including muting of sound/music groups.

You want to make sure you take advantage of Flash’s OOP capabilities and make your model event based as well. This also helps to avoid the dreaded _root calls. In time, as Kongregates framework expands, coding with proper OOP will make it easier to tie in with any APIs and ensure better compatibility. It also makes coding more enjoyable :)

 
Flag Post

thanks arcane, i’ll think about it when i’ll have the OOP skills ;)

thx

 
Flag Post

Hey Indie, sry I missed your questions in chat.

To answer your questions, yes its a flash component that I built. I built a custom UI with it so myself or other devs can interface and set up sounds easily (as you see in the screens). The UI is flash and also uses some JSFL. It also functions as a non component and more like a class library if preferred as no setup in the UI is required. You can just throw it in the library or import the class and call the sounds by code and it will automatically do the rest, whether the UI has been used or not. The UI just makes it easier to set up and change initial default settings (like for volume normalization, etc).

I’ve built a few of these components for easier game development – XML asset managers, preference managers, etc. It makes coding easier and lets me focus on the actual game logic instead of typing the same sound routines for every project.

 
Flag Post

No worries Arcane, actually I had to split earlier also, as I was only on a break… Wow, thanks for elaborating, that sounds like a complex but very useful tool.

I have experience writing custom classes but not components, yet. Right now I am actually learning how to do an event-based model for Flash programming, and that is interesting. OOP is great and you’ve definitely re-sparked my interest in Flash components, so hopefully soon I will understand more of what you’re talking about with JSFL and all that. =P