Playing laser sound while pressing spacebar as3 - actionscript-3

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

Related

How would I play a sound when the collision occurs?

I am new to flash and using as3. I am in the process of making a simple catching game where the items fall from the top and you control a basket at the bottom to catch them. My script is fine and is playing without erros throughout which I am happy about, but how would I add a sound clip to this script to play when the item lands in the basket? Thanks in advance!!!
import flash.events.MouseEvent;
import flash.events.Event;
import flash.text.TextField;
var catcher:Catcher;
var createEnemyID:uint;
var gameSpeed:uint;
var droppedText:TextField;
var caughtText:TextField;
var score:uint=0;
function initGame():void{
catcher=new Catcher();
catcher.x=500;
catcher.y=1400;
addChild(catcher);
stage.addEventListener(MouseEvent.MOUSE_MOVE,moveCatcher);
Mouse.hide();
gameSpeed=500;
createEnemyID=setInterval(createEnemy,gameSpeed);
droppedText=new TextField();
droppedText.x=50;
droppedText.y=50;
addChild(droppedText);
caughtText=new TextField();
caughtText.x=250;
caughtText.y=50;
addChild(caughtText);
droppedText.text=caughtText.text='0';
}
function moveCatcher (e:MouseEvent):void{
catcher.x=this.mouseX;
e.updateAfterEvent();
}
function createEnemy():void{
var enemy:Faller=new Faller();
enemy.y=-1;
enemy.x=Math.random()*stage.stageWidth;
enemy.addEventListener (Event.ENTER_FRAME, dropEnemy);
addChild(enemy);
}
function dropEnemy(e:Event):void{
var mc:Faller=Faller(e.target);
mc.y+=15;
if(mc.hitTestObject(catcher)) {
caught(mc);
}
else if (mc.y>stage.stageHeight){
dropped(mc);
}
}
function caught(mc:Faller):void{
mc.removeEventListener (Event.ENTER_FRAME,dropEnemy);
removeChild(mc);
caughtText.text=String(Number(caughtText.text)+1);
}
function dropped(mc:Faller):void{
mc.removeEventListener (Event.ENTER_FRAME,dropEnemy);
removeChild(mc);
droppedText.text=String(Number(droppedText.text)+1);
if(droppedText.text=='5'){
gameOver();
}
}
function gameOver():void{
score=Number(caughtText.text);
stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveCatcher);
removeChild(catcher);
clearInterval(createEnemyID);
removeChild(caughtText);
removeChild(droppedText);
while(numChildren>0){
getChildAt(0).removeEventListener(Event.ENTER_FRAME,dropEnemy);
removeChildAt(0);
}
Mouse.show();
gotoAndStop('gameover');
}
initGame();
import the sound into flash.
edit the properties and set the class of the sound to MySoundClass or whatever you like but you have to reference it later.
In your code write the following in the collision method.
var sound:Sound = new MySoundClass();
sound.play();
See this AS3 Sound tutorial

as3 - dispatch mouse event from external class

I am having problems understanding correctly how to dispatch events and capture them in another class.
In this case, I am trying to emulate a mouse click dispatched from an "clickM" class.
On stage I have 2 movieclips to test, a custom cursor and the listeners to capture the click event.
clickM:
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event; //dispatcher
import flash.events.MouseEvent;// mouse event
public class clickM extends MovieClip {
private var delay: uint = 3000;
private var repeat: uint = 0; //se va por todo el tiempo
private var myTimer: Timer = new Timer(delay, repeat);
public function clickM() {
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
}
private function timerHandler(e: TimerEvent): void {
//repeat--;
//statusTextField.text = ((delay * repeat) / 1000) + " seconds left.";
trace ( "simulate click...");
//dispatchEvent(new MouseEvent(MouseEvent.CLICK));
this.dispatchEvent(new MouseEvent(MouseEvent.CLICK, true, false));
}
}
}
Stage code, rojo & morado are movieclips:
import flash.events.MouseEvent;
stage.addEventListener(Event.ENTER_FRAME, myFunction);
var mano: clickM = new clickM();
mano.name = "mano";
addChild (mano);
morado.addEventListener(MouseEvent.CLICK, cl);
rojo.addEventListener(MouseEvent.CLICK, cl);
stage.addEventListener(MouseEvent.CLICK, cl);
function myFunction(event: Event) {
mano.x = mouseX;
mano.y = mouseY;
}
function cl(e: MouseEvent) {
trace("click over " + e.target.name);
}
If I click over morado or rojo, there's no problem - I can get their names. If I just let the code run, I can't get their names, I just get "mano", which is the custom cursor I'm using.
How can I get the desired behavior?
Regards.
Add mouseEnabled=false; inside clickM constructor. This should make Flash to ignore your mano in the event dispatch phase, so the underlying object should be the primary target if there's any, otherwise the target will be the stage. If your custom cursor contains more movie clips, you should also add mouseChildren=false;.
public function clickM() {
mouseEnabled=false;
// possibly add this too
mouseChildren=false;
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
}

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

Mute Button in Action Script 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.