AS3/Flash CS6 - Stop music after Scene? - actionscript-3

I'm a beginner in AS3 and Flash CS6.
I tried to search for a solution, but couldn't find anything that would've worked for me.
I have a small game where you must avoid particles coming at you, and as usual, when you hit one, you goto The End scene.
The problem is that when you replay the game from The End screen, the music won't stop, but start again on top of the other one.
I've included my sound from Library -> mysound.mp3 -> Properties -> ActionScript3 -> Export for ActionScript & Export in Frame 1.
Is there a way I could stop the sound from ActionScript?

Is there a way I could stop the sound from ActionScript?
Don't place Sound object on the timeline, control it yourself, It's pretty easy and straightforward.
//Start background music somewhere
var soundFromLibrary: SomeSound = new SomeSound();
var myBackgroundMusic: SoundChannel = soundFromLibrary.play(0, int.MAX_VALUE);
//Now you have reference on background music, stop it
myBackgroundMusic.stop();

If you have the music set on a specific keyframe, then in the properties panel, set it to
Sync: Start
Repeat: 1
And on the frame where you want to stop the music:
Sync: Stop
Repeat: 1

Related

Flash AS3 | Looping sound for each animation set

with University done I've finally had time to go back to practicing my Flash work, and I need some help with getting sound to loop and change depending on what part of the animation is playing.
This is also partly a follow up question to an older one I asked, so look here to get more detail as to where I'm coming from if you don't understand! (I've solved that issue, the current one relates to following up on it!)
Flash AS3 | Finishing current animation set before code is executed
The situation is basic: I have two different scenes, one of a man walking and one of a man running. It loops seamlessly of the man walking until you hit a button, where it finishes the animation then starts looping the run animation. Likewise, hitting the button again, finishes the loop before going back to the looping walk animation. The code for the button is below.
import flash.events.MouseEvent;
Next2.addEventListener(MouseEvent.CLICK, Change_2);
function Change_2(event: MouseEvent): void
{
addEventListener(Event.ENTER_FRAME, enterFrame);
function enterFrame(e:Event):void {
if (currentFrame == 30) {
gotoAndPlay(31);
removeEventListener(Event.ENTER_FRAME, enterFrame);
}
}
}
My issue is trying to get a footstep sound to match up with these animations. So while the man is walking, the 'walking footsteps' sound clip will play and loop alongside the animation, and when the button is pushed and the animation moves into the running animation, the 'running footsteps'' sound will play.
Any help on this would be much appreciated. I'm terrible when it comes to anything audio/code based.
Well u can check about soundChannel. soundChannel can stop() and play() ur audio.
So import ur external sound or use internal one. ur code probably seems like this.
var mySound:Sound = new Sound(new URLRequest("YourSoundPath"));
var sc:SoundChannel = new SoundChannel();
//when u wanna play
sc = mySound.play();
//when u wanna stop
sc = mySound.stop();
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/SoundChannel.html
also look this link for more info
If you want to really MATCH the footsteps to the animation (for example, the step sound should play when the player have put his feet down) the only sure way to do that is to place the sound on a different layer where your animation is and set the sound to play as "stream" (if i remember correctly).
Otherwise the animation might be out of sync because of different computer capabilities, current computer or graphics card load etc.
When the sound is set to play as "stream" the flash player is orienting on the sound and drop frames if the graphics are slow to keep the sound and animation in sync

Overlapping sound in as3

Okay, so I am making a game, and I can't figure out how to keep the sound track from overlapping. When the game comes to a lose or win screen, I will click "Play again" and the sound will overlap. I have the sound on it's own layer. Here's what I have so far.
var snd_SolidState = new SolidState();
snd_SolidState.play(0,1);
And on Layer 2 Frame 2 and 3, I have:
snd_SolidState.stop();
snd_SolidState = null;
It says "stop" is not a function. So how would I get the song to stop on the lose/win screens? SolidState is the song.
If the code is related to the click of the "Play again" Button, here is the code for those.
mc_again.addEventListener(MouseEvent.MOUSE_UP,upAgain);
function upAgain(e:MouseEvent){
gotoAndStop(1);
}
Can anyone help?
You need to use a SoundChannel to stop your music.
The variable snd_SolidState is a Sound object, Sound doesn't have a stop method implemented; The Sound.play() however, returns a SoundChannel object which does...
So you can store that (the SoundChannel object returned by snd_SolidState.play()), which you can later use to stop the song.
PS: I recommend you start typing your variables (ie: var snd_SolidState:Sound;), that way you force yourself to know which object type you're dealing with and can find reference for it; in this case Sound.play().

How to put music in individual scenes and have it change when i select a new scene

Alright I've managed to make an almost web page style thing in flash. I have all the coding to navigate the pages finished, but now I'm having the issue of when I change pages my main page music continues while the other pages music begins. I.,m unsure of how to code this. can anyone help? also I already have click sound effects going for my buttons and they seem to be working fine. Don't know if that helps at all. I'm also using flash cs6 and don't want to go to cc, because the removal of the bone tool.
To stop a sound, you will need to use a SoundChannel object when you tell the sound to play. Then, with the reference to the SoundChannel, you can stop the sound before you exit the section.
// save this reference for later so you can stop the sound when you want..
var mySoundChannel:SoundChannel = mySound.play(); // Get a sound channel.
// Call this later
mySoundChannel.stop();
Without a SoundChannel to control it, the sound object will do its default behavior, which is to play to completion.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/SoundChannel.html

Disabling Audio Flash

When creating an AS3 project and adding audio what I usually do is drag the audio I want to use (for a game menu for example) and when I want to stop the audio I enter
SoundMixer.stopAll();
I do this because if I say go to the about game frame in this menu then I will have the audio, but if I come back to the main frame where I originally had the music it sorta doubles in and the two different timings of the music are playing at the same time.How do I stop this without having to disable the audio when I change frame?
In these cases, it is easier to start and stop the sound directly via code. First, right click the sound clip in your Library panel, and select Properties. In the ActionScript tab, check "Export for ActionScript", and give it a class name of, say, Music. Then, to play and stop this clip, you can use this code:
import flash.media.Sound;
// create the music Sound object so that we can start playing
var music : Sound = new Music();
var musicChannel : SoundChannel;
// play the music
// the play() method returns a SoundChannel,
// which you can later use to stop the music
// notice that we first check to make sure musicChannel is null
// if it isn't null, music is already playing, so don't play it twice
if(musicChannel != null) musicChannel = music.play();
// when you switch screens, stop the music
// clear out the channel, so that we know that it is safe to start the music again
musicChannel.stop();
musicChannel = null;
Alternatively, if you play music and sounds by placing them on the timeline, you can stop it by using the Sync setting of the frame. On the timeline, create a keyframe where you switch screens and want the music to stop playing. Select that frame, and drag the sound clip you want to stop onto that frame. In the Properties panel, change the Sync option to Stop. This will cause only the selected sound to stop playing.
Yet another method is to create an empty MovieClip with the sound placed inside of it. Change the Sync setting to Stream, and pad the clip out with empty frames so that it will play the entire clip. Then, if you place this clip on your main menu screen, then it will only play during the main menu. Stream sounds only play the sound for as long as the MovieClip exists and is playing. Compare this to normal Event sounds, which will play the entire sound regardless.
The latter methods are more old-school and movie-centric. If you are programming a game, it would be more appropriate to use the former method to give you more control of the audio in code.

Adobe Flash CS3 sound in movieclip

There's an animation (MovieClip) in my scene. There's a sound accompanying animation. I inserted it into the first frame of the animation.
I want this animation to be inactive initially. I plan to activate it when some button is pressed.
I don't know how to set it "inactive" from within Adobe Flash workspace, so I just put in constructor of my scene:
MovieClip(getChildByName("MyAnim")).stop();
By it does not prevent that sound to start playing when my scene appears!
How do I manage it?
In my experience, it usually isn't a good idea to insert sounds directly onto the timeline in flash. I strongly advice to import the .mp3 into the Flash program and play it by code using the AS3 Sound class.
EDIT: This way, the sound won't play until you use the Sound Class .play() method, which you could do at the beginning of your animation on the timeline OR from the button that activates it.
Here is a nice Republic of Code tutorial giving you a step by step on how to play a Sound file using the AS3 Sound Class.
Put the sound on frame two in your movieclip. Then go to that frame when you click the button.
private function onButtonClick(event : MouseEvent) : void
{
mySoundClip.gotoAndStop(2);
}