onPlayStatus as3 - actionscript-3

New to this one. Have this code for viewing multiple netstream events. Want to use the onPlayStatus to loop the "flv/intro.flv" and for the rest of the videos, I would like them to return to the intro.flv after they are done playing, but I can't find anything that helps enough. Can anyone offer a link or some help with the function? Here is my code so far :
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
var metaDataListener:Object = new Object();
metaDataListener.onMetaData = function(meta:Object){
}
ns.client = metaDataListener
var myVideo:Video = new Video(800, 600);
myVideo.x = 0;
myVideo.y = 0;
addChild(myVideo);
setChildIndex(myVideo, 0);
myVideo.attachNetStream(ns);
ns.play("flv/intro.flv");
duetBTN.addEventListener(MouseEvent.CLICK, playVideo1);
vantageBTN.addEventListener(MouseEvent.CLICK, playVideo2);
cabrioBTN.addEventListener(MouseEvent.CLICK, playVideo3);
classicBTN.addEventListener(MouseEvent.CLICK, playVideo4);
laundryBTN.addEventListener(MouseEvent.CLICK, playVideo5);
resourceBTN.addEventListener(MouseEvent.CLICK, playVideo6);
industryBTN.addEventListener(MouseEvent.CLICK, playVideo7);
homeBTN.addEventListener(MouseEvent.CLICK, playVideo8);
function playVideo1(e:MouseEvent):void {
ns.play ("flv/duet.flv");
}
function playVideo2(e:MouseEvent):void {
ns.play("flv/vantage.flv");
}
function playVideo3(e:MouseEvent):void {
ns.play("flv/cabrio.flv");
}
function playVideo4(e:MouseEvent):void {
ns.play("flv/classic.flv");
}
function playVideo5(e:MouseEvent):void {
ns.play("flv/laundry.flv");
}
function playVideo6(e:MouseEvent):void {
ns.play("flv/resource.flv");
}
function playVideo7(e:MouseEvent):void {
ns.play("flv/industry.flv");
}
function playVideo8(e:MouseEvent):void {
ns.play ("flv/intro.flv");
}

You can attach an event listener to the NetStream object that detects when a video has finished playing.
var introPlayer:NetStream = new NetStream(nc); // nc refers to shared net connection declared earlier
var introVid:Video = new Video(800, 600);
ns.addEventListener(NetStatusEvent.NET_STATUS, checkStreamStatus);
function checkStreamStatus(e:NetStatusEvent):void {
switch (e.info.code) {
case "NetStream.Play.Complete":
playIntro();
break;
}
};
function playIntro():void {
addChild(introVid);
introVid.attachNetStream(introPlayer);
introPlayer.play("flv/intro.flv");
}
Have changed the code completely to match the set-up you were using originally. This is how you're supposed to do it, apologies for previous answer. This is the correct way of doing it though.

If NetStream.Play.Complete is not dispatched, replace it with:
NetStream.Play.Stop or NetStream.Buffer.Flush

Related

ActionScript3 remove child error

I recently have been converting an as2 fla to as3 (new to AS3) and have the entire thing working on export, but I am getting an error when I try to remove previously loaded swf's before a new swf is loaded
ArgumentError: Error #2025: The supplied DisplayObject
must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at MethodInfo-11()
I know the error relates to my removeChild code here:
`stage.addEventListener(MouseEvent.CLICK, removeSWF);
function removeSWF (e:MouseEvent):void
{
if(vBox.numChildren !=0){
// swfLoader.unloadAndStop();
vBox.removeChild(swfLoader);// empty the movieClip memory
}
}`
However, I cannot seem to find a suitable rewrite for this code that will work and not have an error. This code IS working, so I'm not sure if it would be worth my time to fix this error, or just leave it. I've already messed with it for a couple days, so at this point it's just frustrating me that I cannot fix it.
The stage mouse click listener is useful in this case because I have a back button not shown in this code that clears the loaded swf's before moving to another scene.
Does anyone see a simple solution for this, or do you think it is unnecessary to pursue since the code does what I require?
ENTIRE CODE:
function launchSWF(vBox, vFile):void {
var swfLoader:Loader = new Loader();
var swfURL:URLRequest = new URLRequest(vFile);
swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);
swfLoader.load(swfURL);
function loadProdComplete(e:Event):void {
trace("swf file loaded");
vBox.removeChild(preLoader);
vBox.addChild(swfLoader);
currentSWF = MovieClip(swfLoader.content);
currentSWF.gotoAndPlay(1);
currentSWF.addEventListener(Event.ENTER_FRAME , checkLastFrame);
swfLoader.x = 165;
swfLoader.y = 15;
function checkLastFrame(e:Event):void {
if (currentSWF.currentFrame == currentSWF.totalFrames) {
currentSWF.stop();
// trace("DONE");
}
}
}
var preLoader:loader = new loader();
preLoader.x = 450;
preLoader.y = 280;
vBox.addChild(preLoader);
function onProgressHandler(event:ProgressEvent){
var dataAmountLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
//preLoader.bar.scaleX = dataAmountLoaded/100;
preLoader.lpc.text= int(dataAmountLoaded)+"%";
//trace(preLoader.bar.scaleX );
}
//NEW ERRORS BUT WORKING
stage.addEventListener(MouseEvent.CLICK, removeSWF);
function removeSWF (e:MouseEvent):void
{
if(vBox.numChildren !=0){
// swfLoader.unloadAndStop();
vBox.removeChild(swfLoader);// empty the movieClip memory
}
}
}
var container:MovieClip = new MovieClip();
var currentSWF:MovieClip = new MovieClip();
fall_b.addEventListener(MouseEvent.CLICK, fall_bClick);
function fall_bClick(e:MouseEvent):void {
var swfFile:String = 'load/fall.swf';
launchSWF(container, swfFile);
addChild(container);
}
face_b.addEventListener(MouseEvent.CLICK, face_bClick);
function face_bClick(e:MouseEvent):void {
var swfFile:String = 'load/face.swf';
launchSWF(container, swfFile);
addChild(container);
}
rott_b.addEventListener(MouseEvent.CLICK, rott_bClick);
function rott_bClick(e:MouseEvent):void {
var swfFile:String = 'load/rottgut.swf';
launchSWF(container, swfFile);
addChild(container);
}
//MORE SWFS...
Any advice anyone has is appreciated
First of all function launchSWF(vBox, vFile):void { isn't closed. You've also got function inside functions which is easy enough for you to solve if you click the lines the curly brackets start and end on to track them.
I can't see anything wrong with the code you said has an error but I'm guessing this isn't all the code. If you using Flash Professisonal you can use permit debugging to show the line the error is on.
EDIT: Please note this hasn't been tested as I'm on my mobile writing out code. That being said this should now work:
var container:MovieClip;
var currentSWF:MovieClip;
var swfFile:String;
var swfLoader:Loader;
var preLoader:Loader;
var swfURL:URLRequest;
init();
function init():void {
preLoader = new Loader();
preLoader.x = 450;
preLoader.y = 280;
vBox.addChild(preLoader);
container = new MovieClip();
currentSWF = new MovieClip();
fall_b.addEventListener(MouseEvent.CLICK, fall_bClick);
face_b.addEventListener(MouseEvent.CLICK, face_bClick);
rott_b.addEventListener(MouseEvent.CLICK, rott_bClick);
stage.addEventListener(MouseEvent.CLICK, removeSWF);
}
function launchSWF(vBox, vFile):void {
swfLoader = new Loader();
swfURL = new URLRequest(vFile);
swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);
swfLoader.load(swfURL);
}
function loadProdComplete(e:Event):void {
trace("swf file loaded");
vBox.removeChild(preLoader);
vBox.addChild(swfLoader);
currentSWF = MovieClip(swfLoader.content);
currentSWF.gotoAndPlay(1);
currentSWF.addEventListener(Event.ENTER_FRAME , checkLastFrame);
swfLoader.x = 165;
swfLoader.y = 15;
}
function checkLastFrame(e:Event):void {
if (currentSWF.currentFrame == currentSWF.totalFrames) {
currentSWF.stop();
// trace("DONE");
}
}
function onProgressHandler(event:ProgressEvent) {
var dataAmountLoaded:Number = (event.bytesLoaded / event.bytesTotal * 100);
//preLoader.bar.scaleX = dataAmountLoaded/100;
preLoader.lpc.text = int(dataAmountLoaded)+"%";
//trace(preLoader.bar.scaleX );
}
function removeSWF (e:MouseEvent):void {
if(vBox.numChildren !=0){
//swfLoader.unloadAndStop();
vBox.removeChild(swfLoader);// empty the movieClip memory
}
}
function fall_bClick(e:MouseEvent):void {
swfFile = 'load/fall.swf';
launchSWF(container, swfFile);
addChild(container);
}
function face_bClick(e:MouseEvent):void {
swfFile = 'load/face.swf';
launchSWF(container, swfFile);
addChild(container);
}
function rott_bClick(e:MouseEvent):void {
swfFile = 'load/rottgut.swf';
launchSWF(container, swfFile);
addChild(container);
}
I have this rewritten. I could not get the vBox errors cleared in the original code, and I was getting many other errors with what was posted. The vBox code was seen on a tutorial. I think it was supposed to reference the loader for the preloader and the swf, and vFile was for the actual .swf. The following code preloads multiple swfs and clears them with no errors. I appreciate your help AntBirch. I'm beginning to understand loaders in as3 a little more now.
//LOAD FIRST PIECE ON OPEN (required to removeChild later)
var swfLoader:Loader = new Loader();
var defaultSWF:URLRequest = new URLRequest("load/fall.swf");
swfLoader.load(defaultSWF);
swfLoader.x = 165;
swfLoader.y = 15;
addChild(swfLoader);
//PRELOADER
var preLoader:loader = new loader();
preLoader.x = 450;
preLoader.y = 280;
function loadProdComplete(e:Event):void {
trace("swf file loaded");
removeChild(preLoader);
addChild(swfLoader);
}
function onProgressHandler(event:ProgressEvent){
var dataAmountLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
preLoader.lpc.text= int(dataAmountLoaded)+"%";
}
//BUTTONS
function btnClick(event:MouseEvent):void {
swfLoader.unloadAndStop();
removeChild(swfLoader);
swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);
addChild(preLoader);
var newSWFRequest:URLRequest = new URLRequest("load/" + event.target.name + ".swf");
swfLoader.load(newSWFRequest);
swfLoader.x = 165;
swfLoader.y = 15;;
addChild(swfLoader);
}
// BUTTON LISTENERS
fall.addEventListener(MouseEvent.CLICK, btnClick);
face.addEventListener(MouseEvent.CLICK, btnClick);
rott.addEventListener(MouseEvent.CLICK, btnClick);
angel.addEventListener(MouseEvent.CLICK, btnClick);
ratts.addEventListener(MouseEvent.CLICK, btnClick);
metal.addEventListener(MouseEvent.CLICK, btnClick);
//etc...
//BACK BUTTON
BB3.addEventListener(MouseEvent.CLICK, BB3Click);
function BB3Click(e:MouseEvent):void {
swfLoader.unloadAndStop();
removeChild(swfLoader);
this.gotoAndPlay(1 ,"Scene 2")
}

FLASH AS3 - Loop video continuously

I found this code on the net, and it's almost perfect for my needs - but I need it to continuously loop the clip.
var so: Sound= new Sound();
var audioChannel: SoundChannel= new SoundChannel();
var audioTransform: SoundTransform = new SoundTransform();
var video:Video=new Video(320,240);
var nc:NetConnection=new NetConnection ;
nc.connect(null);
var ns:NetStream=new NetStream(nc);
var meta: Object = new Object();
meta.onMetaData = function(meta:Object){
trace(meta.duration);
};
ns.client=meta;
addChild(video);
video.attachNetStream(ns);
ns.play("vid.flv");
//clip is a transparent clip (alpha=0) for the entire scene
clip.addEventListener(MouseEvent.MOUSE_OVER,mute);
clip.addEventListener(MouseEvent.MOUSE_OUT,unmute);
function mute(e:MouseEvent) {
ns.soundTransform=audioTransform;
audioTransform.volume=0;
}
function unmute(e:MouseEvent) {
ns.soundTransform=audioTransform;
audioTransform.volume=1;
}
To loop a video stream in AS3:
Listen for the streams NET_STATUS event.
//ns is a netstream object
ns.addEventListener(NetStatusEvent.NET_STATUS, streamStatus);
In your streamStatus event handler, check if the status code is the video stop event:
function streamStatus(status:NetStatusEvent) {
if (status.info.code == "NetStream.Play.Stop") {
//the video has stopped
}
}
To loop the video, just seek to the beginning.
ns.seek(0);
So this is the final code:
ns.addEventListener(NetStatusEvent.NET_STATUS, streamStatus);
ns.play("vid.flv");
function streamStatus(status:NetStatusEvent) {
if (status.info.code == "NetStream.Play.Stop") {
ns.seek(0);
}
}
To see whatever status codes you can check for, see the documentation.

eventlistener doesn't execute its function

I need to put some thumbnails on the stage. they have different width and I need to get the width after They are loaded.I used the listener to find the width but the function which listener should call doesnt run.
why my code doesn't enter to the function of loadThumbs?
function makeScroller():void
{
for (var item:uint = 0; item < 7; item++ )
{
var thisOne:MovieClip = new MovieClip();
var blackBox:Sprite = new Sprite();
var thisThumb:Sprite = new Sprite();
var imageLoader:Loader = new Loader();
imageLoader.load(new URLRequest(thumbList[item]));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadThumbs);
function loadThumbs (e:Event):void
{
blackBox.graphics.beginFill(0xFFFFFF);
blackBox.graphics.drawRect(-1,-1,imageLoader.width,imageLoader.height);
thisOne.addChild(blackBox);
thisOne.blackBox = blackBox;
thisOne.x = tsX;
thisThumb.addChild(imageLoader);
thisOne.addChild(thisThumb);
tsX += imageLoader.width;
scroller.addChild(thisOne);
}
}
}
I have just tried a more simplified version of your code without any issues, maybe try this:
NOTE: Best practice is to add the COMPLETE listener BEFORE you call load also...
function makeScroller():void
{
var imageLoader:Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedHandler);
imageLoader.load(new URLRequest("http://images.imagecomics.com/c/2012/IMG120350.jpg"));
function loadedHandler(e:Event):void
{
trace("loaded");
}
}
makeScroller();

Actionscript 3: mouse_over & mouse_out causing Error #1009?

When I mouse over the buttonSprite, it adds displaySprite to the stage, and when I mouse out buttonSprite, the displaySprite is removed.
My problem is that when I swiftly mouse over and out the buttonSprite several times, the displaySprite is not be removed and an error message(Error #1009) is displayed. Even I type "displaySprite = null", it's still not working. Any suggestions? Thanks
var buttonSprite:Sprite = new Sprite();
addChild(buttonSprite);
buttonSprite.addEventListener(MouseEvent.MOUSE_OVER, overSprite);
var displaySprite:Sprite;
function overSprite(e:MouseEvent):void{
displaySprite = new Sprite();
addChild(displaySprite);
buttonSprite.addEventListener(MouseEvent.MOUSE_OUT, outSprite);
}
function outSprite(e:MouseEvent):void{
removeChild(displaySprite);
displaySprite = null;
}
There is no guarantee that the events will fire in order.
In your case you do not have to instantiate the displaySprite multiple times.
Just don't null it out and the object will be there.
var buttonSprite:Sprite = new Sprite();
addChild(buttonSprite);
buttonSprite.addEventListener(MouseEvent.MOUSE_OVER, overSprite);
//you only need to create it once.
var displaySprite:Sprite = new Sprite();
function overSprite(e:MouseEvent):void{
addChild(displaySprite);
buttonSprite.addEventListener(MouseEvent.MOUSE_OUT, outSprite);
}
function outSprite(e:MouseEvent):void{
removeChild(displaySprite);
}
How about this?
var buttonSprite:Sprite = new Sprite();
addChild(buttonSprite);
buttonSprite.addEventListener(MouseEvent.MOUSE_OVER, overSprite);
buttonSprite.addEventListener(MouseEvent.MOUSE_OUT, outSprite);
var displaySprite:Sprite;
addChild(displaySprite);
displaySprite.visible=false;
function overSprite(e:MouseEvent):void
{
displaySprite.visible = true;
}
function outSprite(e:MouseEvent):void
{
displaySprite.visible = false;
}
The problem is MouseEvent.MOUSE_OVER is dispatched multiple times when you mouse over a display object. What you should try is MouseEvent.ROLL_OVER and MouseEvent.ROLL_OUT those two events are dispatched once.
Something along the lines of :
var buttonSprite:Sprite = new Sprite();
addChild(buttonSprite);
buttonSprite.addEventListener(MouseEvent.ROLL_OVER, overSprite);
buttonSprite.addEventListener(MouseEvent.ROLL_OUT, outSprite);
var displaySprite:Sprite;
function overSprite(e:MouseEvent):void
{
if(!displaySprite)
{
displaySprite = new Sprite();
addChild(displaySprite);
}
}
function outSprite(e:MouseEvent):void{
if(displaySprite)
{
removeChild(displaySprite);
displaySprite = null;
}
}

ActionScript 3: How to remove EventListener with anon functions

I have written code as follows.
Problem is that I can't remove Event.COMPLETE event listener and when I call the loadData function twice or more, it works 2 times or more. Sorry for my bad english and worse explanation but I need to fix it today and I don't know what to do.
I think the code is pretty obvious. please help!
var ldr:URLLoader = new URLLoader();
function loadData(text_place, scrollbar, fileURL:String):void {
text_place.wordWrap = true;
var f:TextFormat = new TextFormat();
f.align = TextFormatAlign.RIGHT;
text_place.setTextFormat(f);
ldr.dataFormat = URLLoaderDataFormat.TEXT;
ldr.load(new URLRequest(fileURL));
ldr.addEventListener(Event.COMPLETE, function ldr_complete(evt:Event){
initText(text_place, ldr.data, scrollbar);
});
ldr.addEventListener(IOErrorEvent.IO_ERROR, loadError);
}
function initText(text_place:TLFTextField, fileContent, scrollbar):void {
ldr.removeEventListener(IOErrorEvent.IO_ERROR, loadError);
text_place.htmlText = "";
text_place.tlfMarkup = fileContent;
scrollbar.update();
trace("Data loaded");
}
function loadError(e:IOErrorEvent):void {
trace("Error loading an external file.");
}
just avoid writing function enclosures and extend the scope of the complete function's passed arguments so it can access them.
var ldr:URLLoader = new URLLoader();
var text_place:TextField;
var scrollbar:Object; //or whatever it is
function loadData(text_place, scrollbar, fileURL:String):void
{
var f:TextFormat = new TextFormat();
f.align = TextFormatAlign.RIGHT;
text_place.wordWrap = true;
text_place.setTextFormat(f);
scrollbar = scrollbar;
ldr.dataFormat = URLLoaderDataFormat.TEXT;
ldr.load(new URLRequest(fileURL));
ldr.addEventListener(IOErrorEvent.IO_ERROR, loadError);
ldr.addEventListener(Event.COMPLETE, loadComplete);
}
function initText(text_place:TLFTextField, fileContent, scrollbar):void
{
removeLoaderEventListeners();
text_place.htmlText = "";
text_place.tlfMarkup = fileContent;
scrollbar.update();
trace("Data loaded");
}
function loadError(e:IOErrorEvent):void
{
removeLoaderEventListeners();
trace("Error loading an external file.");
}
function loadComplete(evt:Event):void
{
removeLoaderEventListeners();
initText(text_place, ldr.data, scrollbar);
}
function removeLoaderEventListeners():void
{
ldr.removeEventListener(IOErrorEvent.IO_ERROR, loadError);
ldr.removeEventListener(Event.COMPLETE, loadComplete);
}
if you want to stop listening for an event after it triggered, you can unregister the anonymous listener in itself:
ldr.addEventListener(Event.COMPLETE, function(event:Event):void
{
event.target.removeEventListener(event.type, arguments.callee);
// ... do whatever you need to do here
});
But if you also want to stop listening for other events from the same dispatcher when it completes, such as your IOErrorEvent.IO_ERROR listener, you'd still need a reference to that listener to remove it.
There is a simpler way. Instead of removing event listeners, close the loader.
ldr.close();
Per the documentation:
Closes the load operation in progress. Any load operation in progress
is immediately terminated. If no URL is currently being streamed, an
invalid stream error is thrown.