How to stop a sound and play a sound depending on text values?
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!