Play sound only on the first pass in Flash - actionscript-3

I have an As3 simple game with navigation buttons. It starts off with short intro with sound. When the game loads and the player dies it goes to retry screen with button which when is pressed goes back to the game. The problem is that when that happens flash plays the music from the short intro when returning to the game. How Do I stop this from happening. Currently I have no code for the sound. I just added the music directly to the frame with the intro and that's it. So what code do I write for the sound to execute only once?

I googled 'as3 load sound' and this popped up
http://www.republicofcode.com/tutorials/flash/as3sound/
This will show you how to do it if the sound is in your library or if it's external.
With your audio file in the library right click on it and in properties>actionscript check export for actionscript and give it a class name yourAudioFileName
...then the code to start and stop will be
var mySound:Sound = new yourAudioFileName();
var myChannel:SoundChannel = new SoundChannel();
// start it with
myChannel = mySound.play();
// stop it with
myChannel.stop();

Related

Flash AS3 Making sound stop on certain frame

I'm working on an animation that requires "Next" buttons at the end of each scene.
The movie resumes after the button is pressed. During the pause, I'd like the background music to pause as well. I've tried several solutions but none seemed to work.
I am using Flash Professional CS 5.5
Assume you are using "timeline" instead pure AS3.
At the start of whole project(MySound is your specified sound):
var position:Number = 0;
var mySound:Sound = new MySound();
var sc:SoundChannel;
When you start to play every scene(or press on resume button):
sc = mySound.play(position);
End of scene:
position = sc.position;
sc.stop();

Action Script3 error #1176

I wanna play a track per frame, i got this in the first frame called "Menu" I am new with as3 sound stuff, i have allready done all the game, is a question game based in plataform gaming using a mcPlayer without botons first i wanna play music in the menu diferent of music in all the game---
IF YOU COULD HELP ME WITH A WAY TO PLAY SOUND EFFECTS IN JUMPING AND BRICK HIT IT WOULD BE HELPFUL, maybe a guide or something
var mp3Menu:Sound= new Menu_mp3();
var Canal1: SoundChannel= new SoundChannel();
addEventListener(Event.ENTER_FRAME, musicaMenu)
function musicaMenu (e:Event):void{
if(currentFrame=="Menu")
{
Canal1 = mp3Menu.play();
}
}

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

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.

Actionscript3 play multiple sounds

I am writing a simple Flash space-shooter-type game. Every time the player fires or an enemy is shot I play a sound. The problem is sound appear to be "blocking" - if the player shoots very often (i.e. before previous sound has finished) the new sound will not play until the previous one has finished. Here is my code:
//constructor- pre-load all sounds
fireSound = new Sound();
var req:URLRequest = new URLRequest("fire.mp3");
fireSound.load(req);
//enter frame listener- play sound
//if space bar pressed
fireSound.play();
//if enemy shot
otherSound.play();