How do I stop sound using AS3) - actionscript-3

my sounds in code called sound1 , sound2 my sounds in class called sounda , soundb I hope find answers to fix my problem`
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
stop();
var sound1:sounda = new sounda();
var sound2:soundb = new soundb();
cbox.addItem( { label: "chose"} )
cbox.addItem( { label: "first"} )
cbox.addItem( { label: "sec"} )
cbox.addEventListener(Event.CHANGE,plays);
function plays(e:Event):void
{
if (cbox.selectedItem.label == "first")
{
sound1.play();
//not working
sound2.stop();
}
if (cbox.selectedItem.label == "sec")
{
sound2.play();
//not working
sound1.stop();
}
}
//when I play sound1 then when I play sound2 .. sound1 still playing

to stop all sounds from playing
import flash.media.SoundMixer;
SoundMixer.stopAll();
to stop a single sound, you must using SoundChannel
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLLoader;
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
mySound.load(new URLRequest("myFavSong.mp3"));
myChannel = mySound.play();
// ...
myChannel.stop();

Related

Video not showing using netstream(bytes)

im working on video streaming i have a php api that return the video and then play it using byte array,but video is not showing, but when i look at the trace is hows the downloaded bytes and bytesloaded.Can you give me idea what i'm doing wrong i'm new in actionscript.
import flash.display.Sprite;
import flash.events.NetStatusEvent;
import flash.events.ProgressEvent;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.NetStreamAppendBytesAction;
import flash.net.URLRequest;
import flash.net.URLStream;
import flash.utils.ByteArray;
var video:Video;
var video_nc:NetConnection;
var video_ns:NetStream;
var video_stream:URLStream;
video_nc = new NetConnection();
video_nc.connect(null);
video_ns = new NetStream(video_nc);
video_ns.client = this;
video_ns.addEventListener(NetStatusEvent.NET_STATUS, ns_statusHandler);
video = new Video(1280, 720);
video.attachNetStream(video_ns);
video.smoothing = true;
video_ns.play(null);
video_ns.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
video_stream = new URLStream();
video_stream.addEventListener(ProgressEvent.PROGRESS, videoStream_progressHandler);
video_stream.load(new URLRequest("https://www.xxxx.com/test.php"));
addChild(video);
function ns_statusHandler(event:NetStatusEvent):void
{
trace(event.info.code);
trace("zzz");
}
function videoStream_progressHandler(event:ProgressEvent):void
{
trace(event.bytesLoaded);
var bytes:ByteArray = new ByteArray();
video_stream.readBytes(bytes);
video_ns.appendBytes(bytes);
//trace(bytes);
}

Stopping a sound in AS3

I'm making a little interactive ad for an album. It has 4 draggable song titles, that when dropped on a target, starts the song. Trying to figure out how to stop the song that's playing when a new song is started. Here's my code:
package {
import flash.display.*;
import flash.events.*;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public class Deftones extends MovieClip{
function Deftones() {
swerve1.addEventListener(MouseEvent.MOUSE_DOWN, dragStartS);
swerve1.addEventListener(MouseEvent.MOUSE_UP, dragEndS);
polt1.addEventListener(MouseEvent.MOUSE_DOWN, dragStartS);
polt1.addEventListener(MouseEvent.MOUSE_UP, dragEndS);
rd1.addEventListener(MouseEvent.MOUSE_DOWN, dragStartS);
rd1.addEventListener(MouseEvent.MOUSE_UP, dragEndS);
gauze1.addEventListener(MouseEvent.MOUSE_DOWN, dragStartS);
gauze1.addEventListener(MouseEvent.MOUSE_UP, dragEndS);
var sound1:Sound= new Sound();
var sound2:Sound= new Sound();
var sound3:Sound= new Sound();
var sound4:Sound= new Sound();
sound1.load(new URLRequest("music/Swerve_City.mp3"));
sound2.load(new URLRequest("music/Poltergeist.mp3"));
sound3.load(new URLRequest("music/Romantic_Dreams.mp3"));
sound4.load(new URLRequest("music/Gauze.mp3"));
var channel:SoundChannel= new SoundChannel();
function dragStartS(e:MouseEvent){
e.currentTarget.startDrag();
}
function dragEndS(e:MouseEvent){
e.currentTarget.stopDrag();
if (swerve1.hitTestObject(speaker1)){
channel.stop();
sound1.play(0);
}
else if (polt1.hitTestObject(speaker1)){
channel.stop();
sound2.play(0);
}
else if (rd1.hitTestObject(speaker1)){
channel.stop();
sound3.play(0);
}
else if (gauze1.hitTestObject(speaker1)){
channel.stop();
sound4.play(0);
}
}
}
}
Any help would me much appreciated.
You can use SoundMixer.stopAll()
Stops all sounds currently playing.
You can use SoundChannel class for this, look in this link (Section 4 - Stopping a Sound)

How to remove stream video in next scene AS3

I am making a project with a video in a scene, but when I go to the next scene the video keeps appearing. How can I remove it.
My code so far is:
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Video;
import flash.events.MouseEvent;
var videoConnection:NetConnection = new NetConnection();
videoConnection.connect(null);
var videoStream:NetStream = new NetStream(videoConnection);
videoStream.play("short_jump.flv");
var metaListener:Object = new Object();
metaListener.onMetaData = onMetaData;
videoStream.client = metaListener;
var video:Video = new Video();
video.attachNetStream(videoStream);
stage.addChild(video);
video.x=200;
function onMetaData(data:Object):void
{
play_btn.addEventListener(MouseEvent.CLICK, playMovie);
stop_btn.addEventListener(MouseEvent.CLICK, stopMovie);
}
function playMovie(event:MouseEvent):void
{
videoStream.play("short_jump.flv");
}
function stopMovie(event:MouseEvent):void
{
videoStream.pause();
}
Thanks for your support!
video.attachCamera( null ) stops audio and video but the last frame remains in the video. you should just video.visible = false afterwards so you don't see that frame. there's a bug where clear() doesn't do what it should.

Showing main menu over objects / movieclips?

I am creating a game in Flash and I am creating a main menu for the game (With buttons like 'Play', 'How to Play', 'Hiscores' etc.) and was wondering what is the best way to go about it?
All of my Actionscript code is in external .as files and I've used classes throughout but I was having trouble figuring out how to get it so that the menu will be shown as soon as the game is ran. The main problem is that there are timers in my game that have event handlers attached to them and I was trying to think of the best way to essentially stop these timers until the user actually clicks 'Play', otherwise the objects spawn over the top of the menu and the timer ticks down.
Would stopping the timers but then adding an event handler to the play button to start the timers be a good idea? I am trying to figure out the best way to do this for future reference.
Thank you for any assistance.
Edit: Tried Cherniv's advice, getting some errors.
Main.as:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.ui.Mouse;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.system.LoaderContext;
import flash.display.Sprite;
import flash.net.Socket;
public class Main extends MovieClip {
public static var gameLayer:Sprite = new Sprite;
public static var endGameLayer:Sprite = new Sprite;
public static var menuLayer:Sprite = new Sprite;
public var gameTime:int;
public var levelDuration:int;
public var crosshair:crosshair_mc;
static var score:Number;
var enemyShipTimer:Timer;
var enemyShipTimerMed:Timer;
var enemyShipTimerSmall:Timer;
static var scoreHeader:TextField = new TextField();
static var scoreText:TextField = new TextField();
static var timeHeader:TextField = new TextField();
static var timeText:TextField = new TextField();
public function Main()
{
var mainMenu:myMenu = new myMenu;
addChild(gameLayer);
addChild(endGameLayer);
addChild(menuLayer);
playBtn.addEventListener(MouseEvent.CLICK, startButtonPressed);
}
function startButtonPressed(e:Event)
{
levelDuration = 30;
gameTime = levelDuration;
var gameTimer:Timer = new Timer(1000,levelDuration);
gameTimer.addEventListener(TimerEvent.TIMER, updateTime);
gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timeExpired)
gameTimer.start();
scoreHeader = new TextField();
scoreHeader.x = 5;
scoreHeader.text = String("Score: ");
gameLayer.addChild(scoreHeader);
scoreText = new TextField();
scoreText.x = 75;
scoreText.y = 0;
scoreText.text = String(0);
gameLayer.addChild(scoreText);
timeHeader = new TextField();
timeHeader.x = 490;
timeHeader.y = 0;
timeHeader.text = String("Time: ");
gameLayer.addChild(timeHeader);
timeText = new TextField();
timeText.x = 550;
timeText.y = 0;
timeText.text = gameTime.toString();
gameLayer.addChild(timeText);
var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
scoreHeader.setTextFormat(scoreFormat);
scoreText.setTextFormat(scoreFormat);
timeHeader.setTextFormat(scoreFormat);
timeText.setTextFormat(scoreFormat);
enemyShipTimer = new Timer(2000);
enemyShipTimer.addEventListener("timer", sendEnemy);
enemyShipTimer.start();
enemyShipTimerMed = new Timer(2500);
enemyShipTimerMed.addEventListener("timer", sendEnemyMed);
enemyShipTimerMed.start();
enemyShipTimerSmall = new Timer(2750);
enemyShipTimerSmall.addEventListener("timer", sendEnemySmall);
enemyShipTimerSmall.start();
crosshair = new crosshair_mc();
gameLayer.addChild(crosshair);
crosshair.mouseEnabled = crosshair.mouseChildren = false;
Mouse.hide();
gameLayer.addEventListener(Event.ENTER_FRAME, moveCursor);
resetScore();
}
function sendEnemy(e:Event)
{
var enemy = new EnemyShip();
gameLayer.addChild(enemy);
gameLayer.addChild(crosshair);
}
function sendEnemyMed(e:Event)
{
var enemymed = new EnemyShipMed();
gameLayer.addChild(enemymed);
gameLayer.addChild(crosshair);
}
function sendEnemySmall(e:Event)
{
var enemysmall = new EnemyShipSmall();
gameLayer.addChild(enemysmall);
gameLayer.addChild(crosshair);
}
static function updateScore(points)
{
score += points;
scoreText.text = String(score);
var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
scoreHeader.setTextFormat(scoreFormat);
scoreText.setTextFormat(scoreFormat);
}
static function resetScore()
{
score = 0;
scoreText.text = String(score);
}
function updateTime(e:TimerEvent):void
{
trace(gameTime);
// your class variable tracking each second,
gameTime--;
//update your user interface as needed
var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
timeText.defaultTextFormat = scoreFormat;
timeText.text = String(gameTime);
}
function timeExpired(e:TimerEvent):void
{
var gameTimer:Timer = e.target as Timer;
gameTimer.removeEventListener(TimerEvent.TIMER, updateTime)
gameTimer.removeEventListener(TimerEvent.TIMER, timeExpired)
// do whatever you need to do for game over
}
function moveCursor(event:Event)
{
crosshair.x=mouseX;
crosshair.y=mouseY;
}
}
}
Menu.as:
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.ui.Mouse;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.system.LoaderContext;
import flash.display.Sprite;
import flash.net.Socket;
public class Menu extends MovieClip
{
var mainMenu:Menu = new Menu();
Main.menuLayer.addChild(myMenu);
playBtn.addEventListener(MouseEvent.CLICK, playBtnPressed);
function playBtnPressed()
{
Main.menuLayer.removeChild(myMenu);
dispatchEvent(new Event("playButtonPressed"))
}
}
My menu is a movieclip named myMenu and the class is set as Menu but I get errors such as:
Main.as, Line 50 1120: Access of undefined property playBtn
I gave the button an instance name of playBtn before I converted the menu to a movieclip so not sure what's going on there. I'm probably missing something really easy but it's a bit confusing for me after typing all day.
If you have all of your initialization stuff (including timers initializations) in Main constructor function , so you need to split it to two functions , Main constructor will show the menu , and "start" button will fire the second function , that will include all the initialization stuff

How to resize my imported external swf file to fit(fullscreen) into stage? please give some sample codes

I used this code to import my swf file.now I want my swf file to fill all my stage(fullscreen).please give some sample code.
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.display.StageScaleMode;
var ldr:Loader = new Loader();
var urlReq:URLRequest = new URLRequest("File/game.swf");
ldr.load(urlReq);
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
addChild(ldr);
function loaded(event:Event):void
{
var content:Sprite = event.target.content;
content.scaleX = 1 ;
}
This will stretch the loaded swf to fill the stage:
function loaded(event:Event):void
{
var content:Sprite = event.target.content;
content.scaleX = stage.stageWidth/content.width;
content.scaleY = stage.stageHeight/content.height;
}