Skip to main content
Known Participant
November 16, 2013
Answered

How to stop a sound and play a sound depending on text values?

  • November 16, 2013
  • 1 reply
  • 527 views

I have a text field and a button. When the button is clicked it displays 100 or 0 (vice versa). The value in the text field is saved and when the user opens the application back up, the text field displays the last number (100 or 0). This is the code:

import flash.events.MouseEvent;
import flash.media.SoundChannel;
import flash.ui.Mouse;

var saveDataObject:SharedObject;
var currentScore:Number;
options_mc
.sound_btn.addEventListener(MouseEvent.CLICK, mute);


init
();
function mute(event:MouseEvent)
{

if(currentScore == 100)
{
currentScore
= 0
options_mc
.onoff_txt.text = String(currentScore);

}
else if(currentScore == 0)
{
currentScore
= 100
options_mc
.onoff_txt.text = String(currentScore);
}
saveData
();
}
function init():void
{

saveDataObject
= SharedObject.getLocal("test");
currentScore
= 100;




if (saveDataObject.data.savedScore == null)
{

    saveDataObject
.data.savedScore = currentScore;
}
else
{

    loadData
();
}


}

function saveData():void
{
saveDataObject
.data.savedScore = currentScore;

saveDataObject
.flush();

}

function loadData():void
{
    currentScore
= saveDataObject.data.savedScore;

   
if (options_mc.onoff_txt) options_mc.onoff_txt.text=String(currentScore);
}

On a different layer in the timeline, I have a sound that comes on. I want the sound to come on if the value of the onoff_txt is equal to 100, and I want the sound to stop playing or not play when the value is at 0.

The failed code that I have come up with is this: CheckSound();

function CheckSound():void
{
if(options_mc.onoff_txt.text == "100")
{
tchannel
= theme.play(0,9999);
}
else if(options_mc.onoff_txt.text == "0")
{
tchannel
.stop();
}
}

It doesn't work at all, the sound doesn't come on or anything!

This topic has been closed for replies.
Correct answer Ned Murphy

If you do not call the function at all, the function will not work at all.  You should be calling that function anytime the program changes the values in the textfield.  That happens when the program starts and when the button gets clicked, so work calling that function into those sections of code.

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
November 16, 2013

If you do not call the function at all, the function will not work at all.  You should be calling that function anytime the program changes the values in the textfield.  That happens when the program starts and when the button gets clicked, so work calling that function into those sections of code.

razer65Author
Known Participant
November 16, 2013

Oh okay, I gotcha! But lets say that tchannel stops playing on one frame and another sound starts to play on another (mchannel). How do I make a universal function that stops both mchannel and tchannel?