How to Remove a Sound - actionscript-3

Hey guys so I can't figure this out. I want to Remove a single sound from a Class. So I have a start screen and when you press "Play Game" it takes you to the game but the start screen music keeps playing. Ive tried sound mixer remove all sounds which works but it removes every sound in the game even the Main game music. So i just to remove this single sound which is added in its class like so:
public class mcStartGameScreen extends MovieClip
{
private var sndmainSong:Sound;
public var mcStart:MovieClip;
public function mcStartGameScreen()
{
mcStart.buttonMode = true;
mcStart.addEventListener(TouchEvent.TOUCH_TAP, startOnTouch, false, 0, true)
//To completely end game when back button pushed on android
NativeApplication.nativeApplication.addEventListener( KeyboardEvent.KEY_DOWN, handleKeyDown, false, 0, true);
//create sound object from main song in library
sndmainSong = new DST10Class();
sndmainSong.play();
}
Now i want to remove the sound through the mcStart event listener function which is this:
private function startOnTouch(e:TouchEvent):void
{
dispatchEvent(new Event("START_GAME"));
//Tried null but didnt work either
//DST10Class = null;
}
So is their any easy way i can acccomplish this? Any help will be appreciated thank you!

Get a handler of the playing sound, then call stop() when you need to stop it.
var mainChannel:SoundChannel;
sndmainSong = new DST10Class();
mainChannel=sndmainSong.play();
Then, to stop it you call mainChannel.stop() and your sound will stop. To play it again, you need to create another SoundChannel object via sndmainSong.play().

Related

Expand menu (movieclip) and music in music setting error

I have make a menu in Flash that can expand and there music setting inside.
The music plays when the application starts. To stop the music you must expand the menu and click the music icon.
It's working fine after I open the program and stop the music.
And it's working if I want to play again.
But there's problems after that:
I can't stop the music again and the music playing double in background.
This is my FLA file:
https://drive.google.com/file/d/1DpqdH64kDnI8xN6fBAt3pwi_bIRQ52mT/view?usp=drivesdk
Can anyone tell me the fault of my program? Thanks.
About "music playing double" does your (audio) playback function create a new anything? (eg: = new Sound or = new SoundChannel)? If yes...
Create your audio variables once outside of functions, then use your functions only to stop/start audio playback.
Use new Sound only when loading a new track, once loaded then use one SoundChannel to play/stop that Sound object.
You need a Boolean to keep track of whether a Sound is already playing or not. If true then don't send another .play() command (now gives two sounds to output/speakers).
See if the code logic below guides you toward a better setup:
//# declare variables globally (not trapped inside some function)
var snd_Obj :Sound;
var snd_Chann :SoundChannel = new SoundChannel();
var snd_isPlaying :Boolean = false;
//# main app code
loadTrack("someSong.mp3"); //run a function, using "filename" as input parameter
//# supporting functions
function loadTrack (input_filename :String) : void
{
snd_Obj = new Sound();
snd_Obj.addEventListener(Event.COMPLETE, finished_LoadTrack);
snd_Obj.load( input_filename ); //read from function's input parameter
}
function finished_LoadTrack (event:Event) : void
{
snd_Chann = snd_Obj.play(); //# Play returned Speech convert result
snd_Obj.removeEventListener(Event.COMPLETE, onSoundLoaded);
//# now make your Play and Stop buttons active
btn_play.addEventListener(MouseEvent.CLICK, play_Track);
btn_stop.addEventListener(MouseEvent.CLICK, stop_Track);
}
function play_Track (event:Event) : void
{
//# responds to click of Play button
if(snd_isPlaying != true) //# check if NOT TRUE, only then start playback
{
snd_Chann = snd_Obj.play();
snd_isPlaying = true; //# now set TRUE to avoid multiple "Play" commands at once
}
}
function stop_Track (event:Event) : void
{
//# responds to click of Play button
snd_Chann.stop();
snd_isPlaying = false; //# now set FALSE to reset for next Play check
}

stopping a soundchannel through a movieclip

my question is as follows. I have a sound triggered by clicking on a movieclip, like this:
var audioPlayer: SoundChannel = new SoundChannel();
var SndJuichen : Sound = new Juichen();
bootje.addEventListener(MouseEvent.CLICK, bootjeClick);
function bootjeClick (e:MouseEvent):void{
audioPlayer = SndJuichen.play();
}
}
and i want to stop this by clicking on another movieclip. for the time being, i have not been able to do this.
so I tried it by clicking on a button. like this:
stop_btn.addEventListener(MouseEvent.CLICK, audioStop);
function audioStop (e:MouseEvent):void{
addEventListener(MouseEvent.MOUSE_DOWN, mousedown);
function mousedown (e:MouseEvent){
audioPlayer.stop();
}
}
At first this worked but, but when i tried it later without changing the code it didn't work anymore.
so my question is how do i get the soundchannel to stop?
I have searched for hours already, if you think you have a link that can easily fix my problem its okay too.
sincerely
Drop the very first line var audioPlayer: SoundChannel = new SoundChannel(); and make sure this code is run in the context of some persistent MovieClip, this way audioPlayer will turn into a property of that MovieClip and be accessible from another frame where you decide to stop the sound.

How to control sound/music playblack in AS3

I'm making a game with AS3. I would like to put a different music in each background.
So far, I've succeed to start the music when the player enters in a scene. But if he goes out, the music is still playing...(I want the music to stop when the players leave a scene).
I don't understand why the music is still playing.. Here's my code :
public function newBackground(thisBack:String):void{
var room = back.currentBack;
switch (thisBack){
case "maisonExt":
var mySound:Sound = new exterieur ();
mySound.play ();
My mp3 file is named "exterieur.mp3".
If you can think of anything it would be a great help.
Thanks !
You need to stop the music again. If you don't stop it, it will keep on playing :)
When you call .play it will return a SoundChannel which lets you control the playback -- changing position, stopping the playback, and accessing the SoundTransform which lets you control the volume. Here is some example code for you:
public var currentSoundChannel:SoundChannel;
public function newBackground(thisBack:String):void
{
var room = back.currentBack;
if (currentSoundChannel != null)
{
currentSoundChannel.stop()
}
var nextSong:Sound;
switch (thisBack){
case "maisonExt":
nextSong = new exterieur ();
break;
}
currentSoundChannel = nextSong.play();
}
If you want to learn more about sound, I would recommend this tutorial http://gamedev.michaeljameswilliams.com/2009/03/03/avoider-game-tutorial-9/. It's part of a larger tutorial which teaches you many aspects of game making, taking you from novice to capable of creating a game. You should really check that out.

Why is my button instance forgetting its textfield text and event listener?

I'm working on an assignment due at midnight tomorrow and I'm about to rip my hair out. I am fairly new to ActionScript and Flash Builder so this might be an easy problem to solve. I think I know what it is but I do not know for sure...
I'm developing a weather application. I designed the GUI in Flash CS5. The interface has 2 frames. The first frame is the menu which has a zipcode input and a button instance called "submit". On the second frame, it has another button instance called "change" which takes you back to the first frame, menu.
In Flash Builder 4, I wrote a class to extend that GUI called Application. When Main.as instantiates it, the constructor function runs. However, this was my first problem.
public class Application extends InputBase {
public function Application() {
super();
// Y U NO WORK???
this.gotoAndStop(1);
this.change.tfLabel.text = "Change";
}
}
When I ran debug it tossed a #1009 error saying it could not access the property or method of undefined object. It is defined on frame 2 in Flash CS5. I think this is the problem... Isn't ActionScript a frame based programming language? Like, you cannot access code from frame 2 on frame 1? But, I'm confused by this because I'm not coding on the timeline?
Anyway, I thought of a solution. It kinda works but its ugly.
public class Application extends InputBase {
public function Application() {
super();
// Y U NO WORK???
this.gotoAndStop(1); // when app is first ran, it will stop on the first frame which is the menu frame
setButton(this.submit, "Submit", 1);
setInput(this.tfZipCode);
}
private function submitZip(e:MouseEvent):void {
this.nextFrame();
setButton(this.change, "Change", 2);
}
private function menu(e:MouseEvent):void {
this.prevFrame();
setButton(this.submit, "Submit", 1); // if I comment this out, the submit button will return to the default text label and it forgets it event.
}
private function setButton(button:ButtonBase, string:String="", action:uint=0):void {
button.buttonMode = true;
button.mouseChildren = false;
button.tfLabel.selectable = false;
button.tfLabel.text = string;
switch (action) {
case 1:
button.addEventListener(MouseEvent.CLICK, submitZip);
break;
case 2:
button.addEventListener(MouseEvent.CLICK, menu);
break;
default:
trace("Action code was invalid or not specified.");
}
}
}
Its not my cup of tea to run the set button function every time one of the button instances are clicked. Is this caused by frames or something else I maybe looking over?
I believe you have two keyframes & both have an instance of the button component called button.
If you are working on the timeline try putting the button in a separate layer, with only one key frame.
Something like the following:
Ensures that you are referencing the same button every time & not spawning new ones on each frame.

Flash Gun Simulation

I wanted to know how to make a sound keep playing whilst the mouse is down e.g.machine gun bullets being fired (not on a click), and when the sound completes, I want the reload sound to play on a click?
The second parameter in sound's play method is the loop count. You can specify a large value for this. play method returns SoundChannel object. In your mouse up listener you can use this SoundChannel's stop method to stop the sound.
// instance variable to remember the channel
private var channel:SoundChannel;
private function onMouseDown(evt:MouseEvent):void {
// remember channel and start looping
channel = snd.play(0, 1000000);
}
private function onMouseUp(evt:MouseEvent):void {
// stop the sound stated in mouse down
channel.stop();
}