PDA

View Full Version : [AS3] Music on/off button bug



no one
03-04-2010, 04:36 AM
I was trying to make a button that will allow you to turn on/off the BG music when you click it but currently, there is a problem:

If I turn the button on and waited for 30 second or more and then click it again, it will start playing the music again without stopping the previous one.

This is the code I used:

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

var soundOn:Boolean = false;
var myMusic:music = new music();
var myChannel:SoundChannel;

Music_Btn1.addEventListener(MouseEvent.CLICK,toggl eSound);
Music_Btn1.buttonMode = true;
Music_Btn1.mouseChildren = false;


function toggleSound(e:MouseEvent)
{
if(soundOn)
{
myChannel.stop();
soundOn = false;
Music_Btn1.text.text = "MUSIC OFF";
}
else // sound is off
{
myChannel = myMusic.play();
soundOn = true;
Music_Btn1.text.text = "MUSIC ON";
}

}


This is the swf in the question:

http://www.*************/swf-files/1267673212.swf

And just in case you can't spot anything in the script, this is the original flash file:

http://www.mediafire.com/?ommzu4nzuka

Any idea what cause the bug?

CGIllusion
03-04-2010, 10:07 AM
Must be something wrong with your music class.. I can't spot anything wrong with this code.

no one
03-05-2010, 12:08 AM
If you can't spot anything with that script then this is the original flash file

http://www.mediafire.com/?ommzu4nzuka

Do you find anything unusual in it?