0
votes

I have been trying to get background music working for my game in as3. What I want is to have one soundtrack for menu screens, and another in game. The menu music should not stop or create a new instance when switching between menu frames (frames 2-5 in my program), but should stop when entering game level frames (frames 6-10).

To do this, I created a 'music' layer on the timeline, and made one keyframe on frame 2 and with code:

music=introMusic.play();

where 'music' is a soundChannel variable

However, when navigating to the help menu and then back to the title (frame 2), another instance of the song starts playing, despite not having left the frame of the music layer that the code is in.

I'm aware of how to stop a soundChannel from playing, but I want the sound to continue playing interrupted through all menu screens until the player actually enters a level.

P.S. I'm relatively new to as3, and I assume that the empty space between keyframes (from circle to square) on a layer is just the one frame? Just occurred to me that maybe that's wrong?

Thanks in advance.

1
I'd say drop frames as concept, use Sprite containers as replacement. This way your music will play when either "frame" is displayed, and you can control when to switch it off or change, etc.Vesper
"PS" - no, these are different frames, although the layers continue to exist between the circle and the square. These are best used in a simple animation movie clip, where you can control the delay between those frames by adding empty frames between two keyframes where you draw something.Vesper
@Vesper I have not learned about Sprites yet, if you could either give me a brief explanation or link me to somewhere that explains them, that would be great thanks.Trex
There's nothing better than direct manual, here help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/… It's like a movie clip, but with no frames, whatever you add there, remains there.Vesper

1 Answers

1
votes

You should definitely take the sounds out of your timeline if you want to do something like this. Make sure all of your sounds are in separate sound channels, and use event listeners to detect when a certain sound has stopped (finished) playing. Remember to remove the event listener before you explicitly ask the sound to stop in-game.

[sound channel].addEventListener(Event.SOUND_COMPLETE,replaySound);

function replaySound(e:Event)
{
    [sound channel].play();
}

Something like this would work (I just quickly wrote it so you'll want to check with the as3 docs before you implement it).

Then make sure when you switch frames or whatever (like when you want to play in-game sounds) that you remove the event listener before you stop the sound.

Do the same for any other sounds.

See the soundComplete event on the soundChannel docs for more information.