1

I am trying to stop the song from playing when user clicks on a button, this is my code :

var mySound:MainSound = new MainSound();
var cmyChannel :SoundChannel;

animation_play.addEventListener(MouseEvent.CLICK, playSound);

function playSound(event:Event) {
mySound.play();
}

animation_stop.addEventListener(MouseEvent.CLICK, stopSound);
function stopSound(event:Event) {
mySound.stop();
}


animation_play.addEventListener(MouseEvent.CLICK,    fl_ClickToGoToAndPlayFromFrame_3);

function fl_ClickToGoToAndPlayFromFrame_3(event:MouseEvent):void
{
   gotoAndPlay(2);
}

when i click on the animation_play object it works perfectly fine, it does as how it should be by playing the sound and starting the animation from the specified frame. however if i click on animation_stop object i get an error

TypeError: Error #1006: stop is not a function.

Anyone know how to go about fixing this ?

dark_illusion_909099
  • 1,067
  • 2
  • 21
  • 41

2 Answers2

2

To stop your sound, you have to use a SoundChannel object like this, for example :

function playSound(event:Event): void 
{
    cmyChannel = mySound.play();
}

and

function stopSound(event:Event): void  
{
    cmyChannel.stop();
}

Hope that can help.

akmozo
  • 9,829
  • 3
  • 28
  • 44
2

You need to set the mySound.play() to the cmyChannel object. Then call stop on cmyChannel. Here is the code:

var mySound:MainSound = new MainSound();
var cmyChannel :SoundChannel;

animation_play.addEventListener(MouseEvent.CLICK, playSound);

function playSound(event:Event) {
cmyChannel  = mySound.play();
}

animation_stop.addEventListener(MouseEvent.CLICK, stopSound);
function stopSound(event:Event) {
cmyChannel.stop();
}


animation_play.addEventListener(MouseEvent.CLICK,    fl_ClickToGoToAndPlayFromFrame_3);

function fl_ClickToGoToAndPlayFromFrame_3(event:MouseEvent):void
{
   gotoAndPlay(2);
}
Display
  • 314
  • 1
  • 13