2
votes

So. Without posting a ton of code. Basically what I'm doing is having a few sound's imported into my fla file in flash cs5 using ActionScript3.

I have a soundchannels created and the instances of the sounds created in a main class.

In the constructor function I have the first scene created with addChild. I also initialize a background music sound through a soundchannel.

The problem is, I want a sound effect to play on frame 40 of that first scene. If I just put the sound into the timeline it plays, but after the scene is removed for scene 2 to be inserted, that sound effect continues to play. I've also tried to use actionscript on frame 40 to play the sound, but that also loops after removeChild.

What is the right way to do this? I'd prefer to keep all the code in the main class, but if I have to add the script into the individual scenes I'm up for it. JUST WISH I COULD GET THE SOUND TO STOP LOOPING!!!

3

3 Answers

1
votes

You'll need to use SoundChannel.stop() on whatever instance of SoundChannel is responsible for the sound in question.

Rather than inserting the sound onto the Timeline I suggest loading and playing the sound as per this example which will give you access to the SoundChannel instance you need.

1
votes

I had the same problem, my Parent clip was loading a child with sound that was some frames into the clip (it was delayed). Removing the child only removed the viewed stage but the sound kept on looping. I inserted this code into the frames where the sound was playing in the child movie clip and all was fixed:

if (this.stage == null) {
    SoundMixer.stopAll();
}

now whenever the child is removed the loop stops. I had to do this for every keyframe a sound clip played in the child.

0
votes

removeChild isn't responsible for stopping any sounds, it only removes the displayObject from the child list, removing it visually.

If you want to stop the sound you started, you'll need a reference to that SoundChannel when calling removeChild. Then just use SoundChannel.stop() http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/SoundChannel.html#stop()

I'll need more information to provide a more accurate answer.

You said one of the options you tried is playing the sound from inside the timeline, on frame 40. Then the problem is if the displayObject was already removed, it still plays. In this case, which isn't my favorite solution but should work, you could do this:

// on frame 40
if (this.stage != null) {
  // play sound here 
}

Which means if this displayObject's stage exists (if it's still on stage, that is, was added as a child and wasn't removed yet) then the code inside executes. If suddenly removed though, the sound will still play. You still need the reference to the SoundChannel to stop it. If from inside the object, you could use addEventListener(Event.REMOVED_FROM_STAGE, func) to know when the object was removed, and execute the SoundChannel.stop code inside the function referenced.

-- Edit: Indeed the proper way to do this is not inside the actual timeline

When you create the object, add the event to it:

myObjectWithSoundInside.addEventListener(Event.REMOVED_FROM_STAGE, objRemoved)

function objRemoved (event:Event):void {
  // This is where you SoundChannel.stop()
}

Then when you execute removeChild(myObjectWithSoundInside), the sound should stop, assuming the inside code of objRemoved is correct and referenced well.