Mute Button in Action Script 3 - actionscript-3

So here is my question. I have a expandable banner. When expands a video is starting to play. My task was to insert an on/off button for the sound in the video, and i did that. But the problem is that i can't reach my button because when i try to move the mouse to the button the expandable area disappear because I'm moving to the button areas. Here is the code too.
import flash.events.Event;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.media.SoundTransform;
import flash.media.SoundMixer;
cierre.gotoAndStop(1);
SoundMixer.soundTransform = new SoundTransform(0);
video_player.autoPlay = true;
video_player.source = "video_500x374.f4v";
video_player.addEventListener(Event.COMPLETE, finVideo);
stop();
b2_btn.buttonMode = true;
b2_btn.addEventListener(MouseEvent.MOUSE_OUT, aBanner1);
b2_btn.addEventListener(MouseEvent.CLICK,onClick);
var clicktag=(stage.loaderInfo.parameters.clickTag)? stage.loaderInfo.parameters.clickTag:"http://www.vasava.es";
function onClick(e:MouseEvent){
navigateToURL(new URLRequest(clicktag),"_blank");
}
function aBanner1(e:Event):void{
video_player.stop();
this.gotoAndStop(1);
}
function finVideo(e:Event):void{
video_player.stop();
cierre.play();
}
function setMute(vol) {
var sTransform:SoundTransform = new SoundTransform (0,1);
sTransform.volume = vol;
SoundMixer.soundTransform = sTransform;
}
var Mute:Boolean = false;
mutebutton.addEventListener (MouseEvent.CLICK,toggleMuteBtn);
function toggleMuteBtn (event:Event) {
if(Mute) {
Mute = false;
setMute(0);
}else{
Mute = true;
setMute(1);
}
}

Either put the mute button in the expanded portion of the banner itself, or just have the expanding banner mute the audio automatically with no need for a second button push. Which to use depends on your specific application and client requirements.

Related

replay button in AS3

I am making an e-learning module with multiple frames.
I want to add a refreshbutton, so that a user can reload a frame (with a movieclip), so that he or she can watch it again. I use one layer where I place all my actions.
I tried, the following, but that doesn't work
refresh_btn.addEventListener(MouseEvent.MOUSE_DOWN, goToCurrentPageHandler) ;
function goToCurrentPageHandler (event:MouseEvent) : void
{
SoundMixer.stopAll();
gotoAndPlay();
I also tried:
/*refresh_button*/
refresh_btn.addEventListener(MouseEvent.MOUSE_DOWN, goToCurrentPageHandler) ;
function goToCurrentPageHandler (event:MouseEvent) : void
{
SoundMixer.stopAll();
gotoAndPlay(currentFrame);
But when I press the refresh button it starts playing the next frame.
Can someone please help me.
Thanks!
without refreshing you can try simply insert stop button function inside play buttons function then no need to refresh you should first stop sound chaneel sc inorder to close sound s . then disable and enable play and stop buttons by //object..mouseEnabled = false; , //object..mouseEnabled = true;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
import flash.events.Event;
btn_play.addEventListener(MouseEvent.CLICK, PlayStream);
var sc: SoundChannel = new SoundChannel();
var s: Sound = new Sound(new URLRequest("folder/song .mp3"));
function PlayStream(event: MouseEvent): void {
sc = s.play();
btn_play.mouseEnabled = false;
btn_stop.mouseEnabled = true;
btn_stop.addEventListener(MouseEvent.CLICK, StopStream);
function StopStream(event: MouseEvent): void {
SoundMixer.stopAll();
sc.stop();
s.close();
btn_play.mouseEnabled = true;
btn_stop.mouseEnabled = false;
}
}
stop();
gotoAndPlay(currentFrame);
actually plays the next one because you are saying "get the current position and start playing from there". Your sound is in a movie clip called *frame_1*. So you should use:
frame_1.gotoAndPlay(1);
I.e. your code should look like that:
/*refresh_button*/
refresh_btn.addEventListener(MouseEvent.MOUSE_DOWN, goToCurrentPageHandler) ;
function goToCurrentPageHandler (event:MouseEvent) : void
{
SoundMixer.stopAll();
frame_1.gotoAndPlay(1);
}

Image change when hover for play and stop button in as3 at cs5

i am new to action script and flash,i am creating online radio player. Here i am faced simple problem. i want to change the button image whether it's play or stop button when i hover the image. i was create symbol for each button and created instance name. and i am put the first play button and next play-over button and stop button and stopover button respectively. i was done this all in one layer. here my hover action script
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.events.Event;
/****stop-control:***/
btnstop.addEventListener(MouseEvent.MOUSE_OUT,StopOut);
btnstopo.addEventListener(MouseEvent.MOUSE_OVER,StopOver);
function StopOver(evt:MouseEvent):void
{
btnstopo.visible=false;
btnstop.visible=true;
}
function StopOut(evt:MouseEvent):void
{
btnstop.visible=false;
btnstopo.visible=true;
}
/*****Play control:****/
playbtn.addEventListener(MouseEvent.MOUSE_OUT,PlayOut);
btnplayo.addEventListener(MouseEvent.MOUSE_OVER,PlayOver);
function PlayOver(evt:MouseEvent):void
{
btnplayo.visible=false;
playbtn.visible=true;
}
function PlayOut(evt:MouseEvent):void
{
playbtn.visible=false;
btnplayo.visible=true;
}
var soundfile:URLRequest = new URLRequest('http://live32.radio.com:80/;stream1.mp3');
var channel:SoundChannel = new SoundChannel();
var sTransform:SoundTransform = new SoundTransform();
var isplay=1;
var myMusic:Sound = new Sound();
myMusic.load(soundfile);
channel=myMusic.play();
playbtn.addEventListener(MouseEvent.CLICK,PlayRadio);
btnstop.addEventListener(MouseEvent.CLICK,StopRadio);
function PlayRadio(evt:Event):void
{
if(isplay==0)
{
isplay=1;
/*var myMusic:Sound = new Sound();
myMusic.load(soundfile);
channel=myMusic.play(); */
SoundMixer.soundTransform = new SoundTransform(1);
btnstop.visible=true;
playbtn.visible=false;
}
}
function StopRadio(evt:Event):void
{
if(isplay==1)
{
SoundMixer.soundTransform = new SoundTransform(0);
isplay=0;
btnstop.visible=false;
playbtn.visible=true;
}
}
here my problem is when i am hit the stop button it's working fine. but after stop i need to show the play button. but as per my code it's show the stop button again after stop. i know the reason why it's show stop button. The MOUSE_OUT is the reason for that. i dont know how can i fix that. please clear anyone, thanks advance
i found answer myself. i was missed removeEventlistner when i click the stop or play button.
here if i click stop button remove the stop EventLIstner and Add the Play EventLIstner. like opposite for play button the sample code here
function PlayRadio(evt:Event):void
{
if(isplay==0)
{
isplay=1;
/*var myMusic:Sound = new Sound();
myMusic.load(soundfile);
channel=myMusic.play(); */
btnplay.removeEventListener(MouseEvent.MOUSE_OUT,PlayOut);
btnplayo.removeEventListener(MouseEvent.MOUSE_OVER,PlayOver);
btnstop.addEventListener(MouseEvent.MOUSE_OUT,StopOut);
btnstopo.addEventListener(MouseEvent.MOUSE_OVER,StopOver);
SoundMixer.soundTransform = new SoundTransform(1);
btnstop.visible=true;
btnplay.visible=false;
}
}
function StopRadio(evt:Event):void
{
if(isplay==1)
{
btnstop.removeEventListener(MouseEvent.MOUSE_OUT,StopOut);
btnstopo.removeEventListener(MouseEvent.MOUSE_OVER,StopOver);
btnplay.addEventListener(MouseEvent.MOUSE_OUT,PlayOut);
btnplayo.addEventListener(MouseEvent.MOUSE_OVER,PlayOver);
SoundMixer.soundTransform = new SoundTransform(0);
isplay=0;
btnstop.visible=false;
btnplay.visible=true;
}

AS3 Flash Stubborn Simple Button Not Appearing on Stage

I am using Flash Professional CS6/AS3 to make a simple game. I want a back button, that when clicked takes the player back to the intro screen. Everything else works, but the button will not show on the screen and it will not function when I drag it onto the screen and click it. The back_Btn class is empty and the symbol contains no timeline code.
There are no error messages. I want to reiterate that everything else is working perfectly.
Here's the document class:
package
{
import com.greensock.TweenLite;
import com.greensock.*;
import com.greensock.easing.*;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Stage;
import flash.display.SimpleButton;
public class Main extends MovieClip
{
var back1:back_Btn;
var intro:introScreen;
var game:levelOne;
var stageRef:Stage;
//
var timer:Number = 0;
var holder:int = 0;
var boolean:Boolean = false;
var speed:int;
//
public function Main()
{
init();
}
function init():void
{
stageRef = stage;
back1 = new back_Btn();
stageRef.addChild(back1);
//
game = new levelOne();
//
intro = new introScreen();
stageRef.addChild(intro);
//
back1.x = 425;
back1.y = 0;
intro.x = 0;
intro.y = 0;
intro.lampBtn.addEventListener(MouseEvent.CLICK,startGame);
addEventListener(Event.ENTER_FRAME,startApp);
back1.addEventListener(MouseEvent.CLICK,exitGame);
speed = 0;
function startApp(evt:Event):void
{
intro.x += speed;
if ((boolean == true))
{
speed = 5;
boolean = false;
}
}
}
function startGame(evt:Event):void
{
game = new levelOne ;
stageRef.addChild(game);
game.x = 0;
game.y = 0;
boolean = true;
}
function exitGame(evt:Event):void
{
trace("back button clicked");
removeChild(game);
addChild(intro);
intro.visible = true;
}
}
}
When not dragged onto the stage, the button doesn't appear. When dragged on, the trace statement doesn't fire when clicked and nothing happens. Very strange.
If your back_Btn is empty you cannot see it as there is nothing to see. Besides you addChild 'game' on top of back1. Intro is also on top.

Playing laser sound while pressing spacebar as3

Trying to figure out a code for my ship when i press spacebar and fires the laser a sound will play
private var reqButton:URLRequest = new URLRequest("laser.mp3");
private var buttonSound:Sound = new Sound(reqButton);
if (key.isDown(Keyboard.SPACE))
{
fireBullet();
buttonSound.play();
}
ok this worked the only 2 things i need was to add
import flash.net.URLRequest;
import flash.media.Sound;
You can try this if you have an external sound in an "audio" folder
var reqButton:URLRequest = new URLRequest("audio/button.mp3");
var buttonSound:Sound = new Sound(reqButton);
and then your keypress code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
function keyDown(event:KeyboardEvent):void
{
switch(event.charCode)
{
case 32: //Space Bar
//trace("Space Bar");
buttonSound.play();
break;
}
}

AS3 button firing off multiple times if clicked fast only

ok so, i wrote this code:
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.display.Stage;
stop();
var loader:Loader = new Loader();
var defUrlReq = new URLRequest("indexCoontentLoad.swf");
var urlRequest:URLRequest = new URLRequest();
var myLoadedSwf:MovieClip = null;
var swfStage:Stage = this.stage;
/////////////// INITIATE LOADERS ///////////////
loader.load(defUrlReq);
/////////////// START MAIN HANDLER FUNCTION ///////////////
/////IMPORT DEFAULT SWF /////
loader.contentLoaderInfo.addEventListener(Event.INIT, loadedHandler);
function loadedHandler(event:Event){
myLoadedSwf = event.target.content;
addChild(myLoadedSwf);
trace(myLoadedSwf);
myLoadedSwf.gotoAndPlay("intro");
trace("STEP 1 -- ext def swf loaded");
}
///////END IMPORT. ///////////////
///// START LISTENERS AND THEIR FUNCTIONS /////
load1.addEventListener(MouseEvent.CLICK,btn4Clicked);
load2.addEventListener(MouseEvent.CLICK,btn4Clicked);
load3.addEventListener(MouseEvent.CLICK,btn4Clicked);
///// END LISTENERS /////
///// START FUNCTIONS /////
function btn4Clicked(e:MouseEvent):void { //-- START btn4Loaded
if (e.target == load1 || e.target == load2 || e.target == load3) {
myLoadedSwf.gotoAndPlay("outro");
removeChild(myLoadedSwf);
urlRequest = new URLRequest(e.target.name+".swf");
loader.load(urlRequest);
addChild(myLoadedSwf);
}
}
and it works, once clicked, it does what it has to do. Ofcourse, me trying to break it, i found that if i click the buttons fast, it will re-import the external swfs causing me to have multiple instances of the external swf.
so in short, if i click like normal(slow ) ie like a person that clicked to view a section etc, then its fine, if i click fast or repeated clicking ie like a person that double clicks etc, then the problem occurs.
any ideas how to fix this?
thanks in advance.
edit*** heres a link to test file to show what i mean
http://www.somdowprod.net/4testing/flash/tst
When you set doubleClick to enabled on your movieclip, this will work. The Flash runtime will thencheck for you if it is a double click and only trigger your method once. If you want to listen for the double clicks, you can by changing the event handler.
mySprite.doubleClickEnabled = true;
mySprite.addEventHandler(MouseEvent.CLICK, onClick);
Good luck.
You could try adding a boolean variable that is set to false. Once the .swf is loaded then change that variable to equal true. Then don't let the swf be loaded unless it is set to false. That way it'll only be allowed to be loaded once.
var isLoaded:Boolean = false;
function btn4Loaded(e:Event):void
{ //-- START btn4Loaded
if(!isLoaded)
{
if (e.target == load1 || e.target == load2) {
myLoadedSwf.gotoAndPlay("outro");
removeChild(myLoadedSwf);
urlRequest = new URLRequest(e.target.name+".swf");
loader.load(urlRequest);
addChild(myLoadedSwf);
isLoaded = false;
}
}
} // end btn4Loaded.