adding event listener to stop a sound, when entering new scene - actionscript-3

I am making a game in FlashPro, with three scenes. 1st scene is the "press play scene", scene 2 is the playing scene and scene 3 will be the "game over scene".
I am trying to write the code to stop a sound playing in scene 1 of my game when entering scene 2.
The code to play the sound is this:
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.Event;
var mySound:Sound = new mainMusic();
var mySoundChannel:SoundChannel = new SoundChannel();
mySoundChannel = mySound.play();
Now, I am very new to coding in general. Most examples I have found talk about stoping the sound with the click of a button. In my case, there will never be a Mouse, so I am trying to make an Event Listener that stops the sound when entering the new scene, or when a condition will be true.
I appreciate all help.

Related

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();
}
}

Play sound only on the first pass in Flash

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();

replay btn in flash cs6 using actionscript 3 is not behaving correctly

I have a flash animation that using action script at the end to stop the animation and has a button for the user to click and replay at the animation. When they click and replay the animation the last scenes that were on the screen don't disappear as the animation restarts. My button has an instance name of replay_btn. Any suggestions?
my action script code is
stop();
import flash.events.MouseEvent; function onClick(event:MouseEvent):void { gotoAndPlay(1); } replay_btn.addEventListener(MouseEvent.CLICK, onClick);
This has been answered now. It seem like there was a bug in flash causes the last scenes to hang while replay the start of the animation. The workaround was to make the replay button go forward one frame to a blank white frame and then direct it back to the start.
Assumed the button is inside the movieclip you want to startover.
stop();
import flash.events.MouseEvent;
function onClick(event:MouseEvent):void {
replay_btn.removeEventListener(MouseEvent.CLICK, onClick);
MovieClip(parent).gotoAndPlay(1);
}
replay_btn.addEventListener(MouseEvent.CLICK, onClick);

flash as3 fullscreen browser jams up playback

I have an AS3 project which when the user clicks a button the browser is set to fullscreen. This works great, however the playback sticks after the navigation is sent to the next frame. The MC on frame two plays, but then the playback head doesn't move forward to frame 3. Once escape is pressed, the fullscreen is broken and then the movie skips to where it would have been normally if it hadn't have gotten stuck on frame two (as if the project was running in the background). This problem also occurs without the fullscreen AS3, just using the controls for a projector to go to fullscreen, and it also occurs if the .swf file plays back directly in a flashplayer without the browser. I don't understand, without fullscreen, everything plays back perfectly....
Here's the code on frame 1:
fullscreenMe.addEventListener(MouseEvent.CLICK, fullScreenIt);
function fullScreenIt(event:MouseEvent):void {
MovieClip(root).gotoAndPlay('two');
stage.displayState=StageDisplayState.FULL_SCREEN;}
And here's the code on frame 2:
var titlesTimer = new Timer(11000,1);
titlesTimer.addEventListener(TimerEvent.TIMER_COMPLETE, titlesTimerFinished);
titlesTimer.start();
function titlesTimerFinished(e:TimerEvent):void {
//trace("timer is finished");
MovieClip(root).nextFrame();
}
var creditsSound:credits = new credits();
var creditsChannel:SoundChannel = new SoundChannel();
creditsChannel = creditsSound.play();
And here's the code on frame 3:
import fl.transitions.Tween;
import fl.transitions.easing.*;
function alphaTween(mc,b,f,d) {
var Alpha:Tween = new Tween(mc, "alpha", Regular.easeIn, b, f, d, true);
}
alphaTween(moon1,0,1,7);
var moonTimer = new Timer(1000,7);
moonTimer.addEventListener(TimerEvent.TIMER_COMPLETE, moonTimerFinished);
moonTimer.start();
function moonTimerFinished(e:TimerEvent):void {
//trace("timer is finished");
MovieClip(root).nextFrame();
}
Hmm from the information given I am not sure if the Playhead is in play mode or stop mode in a frame.
Before we can solve it
Is there a this.stop(); command added before any code is written in the frame. This ensures the Playhead is not in playing mode and stops in the present frame. If you want it playing, then no issues. But looking at the timers, I assume you must have added the stop() command.
Documentation for MovieClip for nextFrame says it sends the playhead to the next frame and stops it. This happens after all remaining actions in the frame have finished executing. So is this intended?

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();