mute and unmute button as3 flash - actionscript-3

I'm trying to create a mute and unmute button. I don't want any play/pause button just mute and unmute. So far all I've created is the mute button, the problem is I have no clue as to make an unmute button. I'd also like the mute icon to change its appearance to unmute icon once it's clicked. Any help is greatly appreciated, thanks everyone.(some side comments or explanations would be nice too)
Hope I'm not asking for too much. Here's my code so far
var soundReq:URLRequest = new URLRequest("assets/for_flash.mp3");
var sound:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
sound.load(soundReq);
sound.addEventListener(Event.COMPLETE, onComplete);
function onComplete (e:Event):void
{
sound.play();
}
mute_btn.addEventListener(MouseEvent.CLICK, muteSound);
function muteSound(e:MouseEvent):void
{
SoundMixer.stopAll();
}

Let's assume in your mute_btn instance, you have a child icon with an instance name of muteIcon and the presence of that icon is a visual indicator on whether your sound is presently unmuted or not (Like a circle with a slash through it type icon) and underneath that is a speaker icon.
This would be code then:
//First, you need to assign the result of the sound.play function to the sound channel
function onComplete (e:Event):void{
channel = sound.play();
}
mute_btn.addEventListener(MouseEvent.CLICK, muteBtnClick);
function muteBtnClick(e:MouseEvent):void
{
//check to see if the sound channel exists yet (in case you click the button before the sound loads, otherwise you'll get an error
if(!channel) return;
//toggle the icon's visibility
mute_btn.muteIcon.visible = !mute_btn.muteIcon.visible;
//now check and see if the icon is visible or not
if(mute_btn.muteIcon.visible){
//it is visible, so we should unmute the sound
channel.soundTransform = new SoundTransform(1); //1 is the volume level from 0 - 1
}else{
//it is not visible, so we should mute the sound
channel.soundTransform = new SoundTransform(0); //0 is no volume, 1 is full volumn
}
}
Also, for efficiency, you can change this:
var channel:SoundChannel = new SoundChannel();
To this:
var channel:SoundChannel;
Because creating a new object in that var is pointless as a whole new object gets assigned when you do channel = sound.play();

Related

Actionscript 3 - How do I make new instances draggable?

I am attempting to build a drag and drop game where a user can build something using the images I provide. I will have images in a menu that the user can click and drag to the building area. The user will be able to add however many instances of that image as they want.
I was able to get part of it working. So far, I can click the image and drag it around, and create as many instances as I want. However, I cannot click and drag the image once I have placed it.
When I do a trace to see what the name is, it says that all the new instances are named hillChild1. I tried to make them named hillChild1, hillChild2, etc., but that doesn't seem to work either... not sure that is an issue, though.
Here's my code:
thesubmenu1.hill.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
var myImage:Sprite = Sprite(new Hill_mc());
var i:Number=0; i++;
function onDown(e:MouseEvent):void {
var myImage:Sprite = Sprite(new Hill_mc());
myImage.name = "hillChild"+i;
addChild(myImage);
myImage.x = mouseX;
myImage.y = mouseY;
myImage.startDrag();
myImage.buttonMode = true;
}
function onUp(e:MouseEvent):void {
var myImage:Sprite = Sprite(new Hill_mc());
myImage.stopDrag();
myImage.name = "hillChild";
}
stage.addEventListener(MouseEvent.CLICK, traceName);
function traceName(event:MouseEvent):void { trace(event.target.name); }
myImage.getChild(myImage).addEventListener("mouseDown", mouseDownHandler);
stage.addEventListener("mouseUp", mouseUpHandler);
function mouseDownHandler (e:MouseEvent):void{
myImage.startDrag();
}
function mouseUpHandler (e:MouseEvent):void{
myImage.stopDrag();
}
I am new to StackOverflow and also Actionscript 3, if it isn't apparent.
Your issue is likely that you are creating a new instance on mouse up (when what you want is a reference to instance that was already created on mouse down). Also, you never add a click listener to you new objects. Add the mouse up listener to stage only after the mouse down (then remove the listener in the mouse up).
thesubmenu1.hill.addEventListener(MouseEvent.MOUSE_DOWN, createCopy);
var i:int=0;
var tmpImage:Sprite; //to store which image is being dragged currently
function createCopy(e:MouseEvent):void {
tmpImage = new Hill_mc();
tmpImage.name = "hillChild"+(i++); //increment every copy
addChild(tmpImage);
tmpImage.x = mouseX;
tmpImage.y = mouseY;
tmpImage.startDrag();
tmpImage.buttonMode = true;
tmpImage.addEventListener(MouseEvent.MOUSE_DOWN, onDown); //add the mouse down to this new object
stage.addEventListener(MouseEvent.MOUSE_UP, onUp); //since the mouse is currently down, we need to listen for mouse up to tell the current copy to stop dragging
}
//this will be called when click a copy
function onDown(e:MouseEvent):void {
tmpImage = Sprite(e.currentTarget); //get a reference to the one that was clicked, so we know which object to stop dragging on the global mouse up.
stage.addEventListener(MouseEvent.MOUSE_UP, onUp); //listen for the mouse up
tmpImage.startDrag();
}
function onUp(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_UP,onUp); //now that the mouse is released, stop listening for mouse up
tmpImage.stopDrag(); //stop dragging the one that was clicked
}

Toggle sound button?

Say I have two buttons, green and red. How do I make it so that when the green button is clicked once, it stays red until it is clicked again? I want to make a button to mute and unmute music. I am using SoundChannel to play my music i.e.
public var intro:IntroSound = new IntroSound();
public var soundControl:SoundChannel = new SoundChannel();
soundControl = intro.play(0, 100);
Thanks.
Put them in a container an toggle visibility of the top (green) Button!
Im assuming u have the buttons instantiatet, im calling them greenButton and redButton.
var container:Sprite = new Sprite();
container.addChild(redButton);
container.addChild(greenButton);
container.addEventListener(MouseEvent.CLICK, onClick);
function onClick(event:MouseEvent):void
{
greenButton.visible = !greenButton.visible; // toggle visibility of green button
//and do whaterver u need on click
}

Stopping the sound of a child swf

The timeline of my child swf, there are layers with specific sound files in them to be in sync with my animation. Now the problem arises when I try to import this swf into my main flash website using a loader and the sound will continuously play everytime the button is clicked in the parent. My question is how do I get the sound to completely clear itself and restart from frame 0 of the child swf upon every click of the button that loads the child into the loader within the parent.
var myLoader:Loader = new Loader();// create a new instance of the Loader class
var project1:URLRequest=new URLRequest("Projects/Q1/Flash_Projects/Greeting_Card/GreetingCard.swf");
var project2:URLRequest=new URLRequest("Projects/Q1/Flash_Projects/Landscape/Landscape.swf");
var project3:URLRequest=new URLRequest("Projects/Q1/Flash_Projects/SpaceInvadersTribute/Main.swf");
var project4:URLRequest=new URLRequest("Projects/Q1/Flash_Projects/RandomImageProducer/RndImgProd.swf");
//var project5:URLRequest = new URLRequest("Projects/Q1/Flash_Projects/Tutorial/Main.swf");
var project6:URLRequest=new URLRequest("Projects/Q1/Flash_Projects/Soundboard/Main.swf");
btnQ1P1.addEventListener(MouseEvent.CLICK,Greeting);
btnQ1P2.addEventListener(MouseEvent.CLICK,landscape);
btnQ1P3.addEventListener(MouseEvent.CLICK, tribute);
btnQ1P4.addEventListener(MouseEvent.CLICK, slideshow);
//btnQ1P5.addEventListener(MouseEvent.CLICK, tutorial);
btnQ1P6.addEventListener(MouseEvent.CLICK, soundboard);
addChild(myLoader);
function Greeting(event:MouseEvent):void {
SoundMixer.stopAll();
myLoader.load(project1);
myLoader.x=550;
myLoader.y=130;
}
//Errors with Sound clips
function landscape(event:MouseEvent):void {
SoundMixer.stopAll();
myLoader.load(project2);
myLoader.x=440;
myLoader.y=130;
}
function tribute(event:MouseEvent):void {
SoundMixer.stopAll();
myLoader.load(project3);
myLoader.x=550;
myLoader.y=170;
}
//Errors with slideshow Code!
function slideshow(event:MouseEvent):void {
SoundMixer.stopAll();
myLoader.load(project4);
myLoader.x=530;
myLoader.y=130;
}
//function tutorial(event:MouseEvent):void{
//SoundMixer.stopAll();
//myLoader.unload();
//myLoader.load(project5);
//myLoader.x = 440;
//myLoader.y = 130;
//}
function soundboard(event:MouseEvent):void {
SoundMixer.stopAll();
myLoader.load(project6);
myLoader.x=550;
myLoader.y=130;
}
I had this same problem.
You need to change the sound from "Event" to "Stream". In Flash Professional, this can be accessed from the properties tab of the sound on the timeline. Click on the sound, and then go to the Properties tab and change the type from Event to Stream.
Now, the sound will stop when the timeline is stopped.

How can you stop a sound from playing in as3?

I have this code in the second frame to play a song, and it works fine. The class of the sound in my library being "MySound".
var snd:MySound = new MySound
snd.play();
Now I need the song to stop playing from a latter frame.
You have to use SoundChannel class for this:
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
mySound.load(new URLRequest("myFavSong.mp3"));
myChannel = mySound.play();
// ...
myChannel.stop();
Reference: http://www.republicofcode.com/tutorials/flash/as3sound/ (Section 4 - Stopping a Sound)
//*This code for playing the sound after import to the library*
var mySound:Sound = new MenuMusic();
var myChannel:SoundChannel = new SoundChannel();
myChannel = mySound.play();
//*This code for the nextframe button*
btn_easy.addEventListener(MouseEvent.MOUSE_DOWN, btneasy);
function btneasy(event:MouseEvent):void
{
gotoAndPlay(62);
myChannel.stop();
}
Reminder :
You don't have to drag the sound file to the timeline . Just paste the
code to the designated Action Script of the button.
var mySound:Sound = new MenuMusic();
'MenuMusic' can be changed what you want. Just Right click the sound
file in the library then select Properties . Click ActionScript Tab and
check 'Export for ActionScript' and you can now changed the name in
'Class' Input Box..
It works so fine to me :)
Hope it solved the problem ! :D

Recyclable Sound Object in Actionscript 3?

Using ONE Sound() object in Actionscript3, how can I play a one MP3 and then, when the user chooses a different one, play a second sound, using the SAME Sound() object?
EDIT: See my answer for how I did it.
You cannot use same Sound object to play multiple files.
Once load() is called on a Sound object, you can't later load a different sound file into that Sound object. To load a different sound file, create a new Sound object.
Ok, I actually did it using the following code. My bug was somewhere else in the FLA file, but this works. I made an uninitialized global variable and created the Sound() object LOCALLY inside of a function. While I'm technically using multiple sound objects, my references are all pointing to ONE object. Additionally, I can call these methods over one another for easier coding. This works for me:
/* -------------
Sound player
functions
------------ */
var snd:Sound; //the sound object
var sndC:SoundChannel; //the soudchannel used as "controller"
var sndT:SoundTransform; //soundTransform used for volume
var vol:Number = 1; //the volume of the song
var pan:Number = 0; //panning of the sound
var pos:Number = 0; //position of the song
var currentSound:String; //currently playing song?
function playSound(s:String){ //this function resets the sound and plays it
stopSound(sndC); //stop the sound from playing
snd = new Sound(); //reset the sound
snd.load(new URLRequest(s)); //load the desired sound
sndC = new SoundChannel(); //(re-)apply the sound channel
applyVolume(vol,pan,sndT,sndC); //apply the volume
sndC = snd.play(pos); //play it
sndC.addEventListener(Event.SOUND_COMPLETE, startSound); //remind it to restart playing when it's done
} //end function
function applyVolume(n:Number, p:Number, st:SoundTransform, sc:SoundChannel){ //takes an argument for the volume, pan, soundTYransform and soundChannel
sndT = new SoundTransform(n,p); //applies the soundTransfrom settings
sndC.soundTransform = sndT; //and attaches it to the soundChannel
} //end function
function stopSound(sndC:SoundChannel){ //this function stops a sound from playing
if(sndC != null){ //if the sound was used before (ie: playing)
if(currentLabel == "video-frame"){ //if we are in the video frame
pos = sndC.position; //store the position of the song to play from at a later time
}else{ //otherwise
pos = 0; //set the position at 0
} //end if
sndC.stop(); //stop it
} //end if
} //end function
function startSound(snd:Sound){ //restarts a sound when it's playing
if(snd != null){ //if the sound exists
sndC = snd.play(pos); //play it
} //end if
} //end function