PDA

View Full Version : Music Button newbie question



johnnien
December 7th, 2009, 05:42 PM
Do you guys know a nice tutorial about a music button in AS 3? I already have this file from another thread here, where a sound button is made on a single frame and the code goes like this:
import flash.media.Sound;
import flash.media.SoundChannel;

var soundOn:Boolean = true; //music is ON when we start
var myMusic:TitleMusic = new TitleMusic();
var myChannel:SoundChannel = myMusic.play(0,1000); // endless loop, in effect
var myTransform:SoundTransform;

mySoundButton.addEventListener(MouseEvent.CLICK,to ggleSound);
mySoundButton.buttonMode = true;
mySoundButton.mouseChildren = false;


function toggleSound(e:MouseEvent)
{
if(soundOn)
{
// turn sound off
myTransform = new SoundTransform();
myTransform.volume = 0; // silent
myChannel.soundTransform = myTransform;
soundOn = false;
mySoundButton.myButtonText.text = "click to turn sound ON";
}
else // sound is off
{
// turn sound on
myTransform = new SoundTransform();
myTransform.volume = 1; // full volume
myChannel.soundTransform = myTransform;
soundOn = true;
mySoundButton.myButtonText.text = "click to turn sound OFF";
}

}
The problem is that I want to gave NO music at the start of the scene and when the button is pressed the music to start playing. What do I need to change here? Or i need to start the whole thing from scratch?
I have attached the file ............... Thanks!

far100
December 13th, 2009, 08:52 PM
before the function add the lines that play the sound that were in the function as such.



myTransform = new SoundTransform();
myTransform.volume = 0; // silent
myChannel.soundTransform = myTransform;
soundOn = false;
mySoundButton.myButtonText.text = "click to turn sound ON";

function toggleSound(e:MouseEvent)

Now I thought it was too much code - perhaps others can correct me. The way I would have done it is so the sound is not continuously playing is:


import flash.media.Sound;
import flash.media.SoundChannel;

var soundOn:Boolean = true; //music is ON when we start
var myMusic:TitleMusic = new TitleMusic();

mySoundButton.addEventListener(MouseEvent.CLICK,to ggleSound);
var myChannel:SoundChannel = myMusic.play(0,1000); // endless loop, in effect

mySoundButton.buttonMode = true;
mySoundButton.mouseChildren = false;

function toggleSound(e:MouseEvent)
{
if(soundOn)
{
// turn sound off
SoundMixer.stopAll();
soundOn = false;
mySoundButton.myButtonText.text = "click to turn sound ON";
}
else // sound is off
{
// turn sound on
myMusic.play(0,1000);
soundOn = true;
mySoundButton.myButtonText.text = "click to turn sound OFF";
}

}