Loading multiple external swf and navigate using buttons on child - actionscript-3

I'm trying to load and unload many external swf files into main blank swf. And use navigation buttons (next/back) that located on the externally loaded child pages to navigate (load and unload pages).
I tried to dispatch event from child to main to unload. It works fine only with the first page, as the dispatcher is bubbling only once.
I have a default page to be loaded and dispatcher listener at the main swf as follows:
var cont:Sprite= new Sprite();
var swfLoader:Loader = new Loader();
var swfFile:URLRequest = new URLRequest("Page1.swf"); //default page
addChild(cont);
swfLoader.load(swfFile);
cont.addChild(swfLoader);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoadedHandler);
function swfLoadedHandler(event:Event):void {
MovieClip(event.currentTarget.content).addEventListener("eventTriggered", removeLoader);
}
function removeLoader(event:Event):void {
SoundMixer.stopAll();
this.swfLoader.unloadAndStop();
this.removeChild(cont);
this.cont = null;
swfLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, swfLoadedHandler);
trace ("dispatched");
}
Also the next button code on child page1.swf is as follwos:
nxt_pg.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_8);
function fl_MouseClickHandler_8(event:MouseEvent):void {
content_mc.gotoAndStop(1);
dispatchEvent(new Event("eventTriggered", true));
SoundMixer.stopAll();
//Code of Loading new page here
}
And the same code for back button.
So, I wonder if anyone could help me with code or better technique..Thanks in advance fore your help.

I worked around this by removing Next and Back buttons from child SWFs and put it on container inside movie clip and change its visibility as needed.
At index page (page0.swf) I simply set 'buttons_mc.visible = false;' and make it visible at rest of pages.
The most important thing here is using 'addChildAt(myLoader,0)' instead of 'addChild(myLoader)' to make the buttons movie clip on top of the loaded SWFs.
All this done only on container code and no need to do any extra coding with the external loaded SWFs.
Below is the code for the container swf:
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;
var swfNumber=0;
var myLoader:Loader = new Loader();
var myRequest:URLRequest = new URLRequest("swf/"+"Page"+swfNumber+".swf");
buttons_mc.visible = false;
myLoader.load(myRequest);
addChildAt(myLoader,0);// will be added under the buttons layer
trace(getChildIndex(myLoader));
//==============
buttons_mc.nxt_pg.addEventListener(MouseEvent.CLICK, nextSwf);
function nextSwf(event:MouseEvent){
if(swfNumber<totalSwf){swfNumber++}
else (swfNumber=0);
reload();
trace("Next");
}
buttons_mc.prv_pg.addEventListener(MouseEvent.CLICK, previousSwf);
function previousSwf(event:MouseEvent){
if(swfNumber>1){swfNumber--}
else (swfNumber=0);
reload();
trace("Back");
}
//==============
function reload(){
if (swfNumber==0){buttons_mc.visible = false;}
else (buttons_mc.visible = true);
myLoader.unloadAndStop();
removeChild(myLoader);
myRequest= new URLRequest("swf/"+"Page"+swfNumber+".swf");
myLoader.load(myRequest);
addChildAt(myLoader,0);
}

Related

Go to specific frame after external swf has been loaded AS3

I have been looking for solutions around here but I can't seem to get it right.
Basically I am trying to load an external swf after clicking on a 'Next' button and it will automatically go to a specific frame eg. frame 8 instead of frame 1.
At first I've got an error of using of using MovieClip function in a Loader and such.
Here's my code
nextBtn.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_1);
var fl_Loader1:Loader;
var fl_ToLoad1:Boolean = true;
function fl_ClickToLoadUnloadSWF_1(event:MouseEvent):void
{
if(fl_ToLoad1)
{
fl_Loader1 = new Loader();
fl_Loader1.load(new URLRequest("projectnowd.swf"));
addChild(fl_Loader1);
var fl_Loader1:MovieClip = event.target.content as MovieClip;
fl_Loader1.gotoAndStop(8);
}
else
{
fl_Loader1.unload();
removeChild(fl_Loader1);
fl_Loader1 = null;
}
fl_ToLoad1 = !fl_ToLoad1;
}
You can access content of loaded swf only after event. Complete was dispatched while loading swf file on to the stage.
Define event handler method to start from 8 th frame
function loaderCompleteHandler(evt:Event):void {
var loadedMovie:MovieClipp = evt.currentTarget.content as MovieClip;
loadedMovie.gotoAndStop(8);
}
Replace if block with below lines of code
if(fl_ToLoad1)
{
fl_Loader1 = new Loader();
fl_Loader1.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
fl_Loader1.load(new URLRequest("projectnowd.swf"));
addChild(fl_Loader1);
}
Happy coding :)

AS3 maintime delay script while external swf loads and plays

I'm having an issue with playing an external SWFs inside the main SWF. Everything loads great, except not on cue. I'm using a simple delay actionscript to pause the main timeline until the external SWFs load, but it doesn't always sync up when testing in browsers.
Is there an AS3 code that I can use to pause the main timeline until the external SWF is finish loading and playing?
I need to do this multiple times through the movie, btw...
Below is the delay and the loadmovie array I'm using.
<--------------//Timer//--------------->
var timer4:Timer = new Timer(1500, 1);
timer4.addEventListener(
TimerEvent.TIMER,
function(evt:TimerEvent):void {
play();
}
);
timer4.start();
<--------------//loadMovie//--------------->
function startLoad()
{
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest('flip/note_flip.swf');
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
mLoader.load(mRequest);
}
function onCompleteHandler(loadEvent:Event)
{
addChild(loadEvent.currentTarget.content);
}
function onProgressHandler(mProgress:ProgressEvent)
{
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
trace(percent);
}
startLoad();
The load() method of the loader object is asynchronous. It means that the process will continue to execute next lines of code and loading is done independently. That's why you can see it's not on cue.
The simple way to fix your problem is to stop() all the loaded movies upon Event.COMPLETE and play() them once all the movies are loaded.
Use an array to store the loaded movies and address them once all movies are loaded.
And yeah, the timer hack is pointless, you won't know how long it will take the file to load, it can be different every time.
EDIT:
Let's assume that you've got N movie .swf files that makes a complete scene. You would like to load all of them, and once they're all loaded - play them.
Let's build a simple step-by-step logics:
1) First of all, we need a list of the URL pointing to where the external SWFs are.
2) Then we want to start loading the SWFs one by one (and not altogether) to prevent Flash Player bug of not loading some of the content AND keeping the order of movie clips aka z-index, depth, layer order etc.
3) On each loaded movie we count how many are still left to be loaded.
4) Once all movies are loaded, we play() all these movies.
Here's the code for you (written in a frame in case if you're not using classes and not familiar with OOP):
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.MovieClip;
var linkArray:Array = ["1.swf", "2.swf", "3.swf"];
var loadedMovieArray:Array = [];
var totalMovies:int = linkArray.length;
var loadedMovies:int = 0;
var urlRequest:URLRequest = new URLRequest();
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, onMovieLoaded);
loadMovies();
function loadMovies():void
{
var movieIndex:int = loadedMovies;
var movieURL:String = linkArray[movieIndex];
urlRequest.url = movieURL;
l.load(urlRequest);
}
function onMovieLoaded(e:Event):void
{
var loadedMC:MovieClip = l.content as MovieClip;
loadedMC.stop();
loadedMovieArray.push( loadedMC );
loadedMovies++;
if (loadedMovies == totalMovies)
onAllMoviesLoaded();
else
loadMovies();
}
function onAllMoviesLoaded():void
{
for (var i:int = 0; i < loadedMovieArray.length; i++) {
var mc:MovieClip = loadedMovieArray[i];
addChild(mc);
mc.play();
}
}

CS5+AS3 Preloader starts at 50%; no clue why

I know there are a lot of previous topics about preloaders and I've tried to follow every one of them but I still get the same problem (well they have helped me go from 80% -> 50%)
Right now it starts at 61450 / 125207 which is about 50%.
Here is my Main Document (default class file for the entire project) class:
public class MainDocument extends MovieClip
{
private var preloader:Preloader;
private var sB:startButton;
public function MainDocument()
{
preloader = new Preloader();
preloader.x = 300;
preloader.y = 400;
addChild(preloader);
loaderInfo.addEventListener(Event.COMPLETE,addStartButton,false,0,true);
}
private function addStartButton(e:Event):void
{
sB = new startButton();
sB.x = 300;
sB.y = 450;
sB.addEventListener(MouseEvent.CLICK,sMainMenu,false,0,true);
addChild(sB);
loaderInfo.removeEventListener(Event.COMPLETE,addStartButton);
}
private function sMainMenu(e:Event):void
{
sB.removeEventListener(MouseEvent.CLICK,sMainMenu);
removeChild(sB);
removeChild(preloader);
sB = null;
preloader = null;
var menuScreen = new MenuScreen();
addChild(menuScreen);
//I have heard that the following code might work better:
//var menuScreen:Class = getDefinitionByName("MenuScreen") as Class;
//addChild(new menuScreen() as DisplayObject);
}
}
And the Preloader that it attaches:
public class Preloader extends MovieClip
{
public function Preloader()
{
addEventListener(Event.ENTER_FRAME,Load);
}
private function Load(e:Event):void
{
//"bar" is a movieclip inside the preloader object
bar.scaleX = loaderInfo.bytesLoaded/loaderInfo.bytesTotal;
//"percent" is a dynamic text inside the preloader object
percent.text = Math.floor(loaderInfo.bytesLoaded/loaderInfo.bytesTotal*100)+"%";
trace(loaderInfo.bytesLoaded+" / "+loaderInfo.bytesTotal);
if (loaderInfo.bytesLoaded == loaderInfo.bytesTotal)
{
removeEventListener(Event.ENTER_FRAME,Load);
}
}
}
-> Nothing is set to Export on Frame 1 except for the Preloader
-> No objects exist on the first frame; the only code on first frame is stop();
-> I placed a copy of every single MovieClip in the second frame and when the startButton is clicked, a gotoAndStop(3); is run so no one ever sees frame 2.
If anyone knows of anything simple that I could have forgotten, please let me know!
Thanks!
You're tying to use a preloader in the file being preloaded. In that case, there is going to be bloat from the rest of the project's code and assets. The reason you are seeing your preloader seemingly delayed is because a swf must load completely before any code will execute. This includes all assets on stage regardless of what frame they are on, even if you have settings in place to export on something other than frame 1. Instead, try using a blank shell as your preloader. This shell will have nothing in it but the loader code and a preloader graphic or animation. When the load is finished, hide your preloader and add your loaded content to the stage of the shell, or a container movieclip in the shell.
All the following code goes in your shell, which is just another FLA file with nothing in it but this code, and a preloader bar. The dimensions of this file should be the same as the file you are loading into it, ie your original swf file you were trying to preload.
Use it by calling loadSwf( "mySwfNameOrURLToSwf.swf" );
The variable _percent will populate with the current load percentage, which you can correspond to your loading bar scale. Presuming the preloader bar is named "bar", the line bar.visible = false; in the onSwfLoaded function will hide it. addChild( _swf ) adds the loaded swf to the shell's stage. The line _swf.init(); references a function in the loaded swf you will need to add called init() that starts your loaded swf doing whatever it is its supposed to do. Have everything in the loaded swf start on the first frame now, including the init() function.
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.SecurityDomain;
import flash.system.LoaderContext;
import flash.system.Security;
import flash.events.Event;
import flash.events.ProgressEvent;
var _swfLoader:Loader;
var _swf:DisplayObject;
var _percent:Number;
function loadSwf( swfURL:String ):void
{
_swfLoader = new Loader();
var req:URLRequest = new URLRequest( swfURL );
var loaderContext:LoaderContext = new LoaderContext();
loaderContext.applicationDomain = ApplicationDomain.currentDomain;
loaderContext.checkPolicyFile = true;
_swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onSwfProgress);
_swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSwfLoaded);
_swfLoader.load(req, loaderContext);
}
function onSwfProgress( evt:Event ):void
{
_percent = Math.round( ( evt.target.bytesLoaded / evt.target.bytesTotal ) * 100 );
}
function onSwfLoaded( evt:Event ):void
{
_swfLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onSwfProgress);
_swfLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onSwfLoaded);
_swf = _swfLoader.content;
addChild( _swf );
bar.visible = false;
_swf.init();
_swfLoader = null;
}
your code looks ok.
when you are not on a http server, loading process are simulated.
After compiling, press crtl + B.
In menu you can choose the downloading speed and simulate a download by pressing again ctrl+enter.
it might help you to debug your preloader
#Lee Burrows What you said was right but would have been better if you looked at what I mentioned at the end of the code (three bold points)
The solution I used was:
-> I set everything to Export on Frame 2 on my 3 frame document.
-> Removed everything on Frame 2
-> Created a TextField via constructor and used drawRectangle for loading bar
-> No movieclips present on frame 1, and used
var menuScreen:Class = getDefinitionByName("MenuScreen") as Class;
addChild(new menuScreen() as DisplayObject);
instead of the previous code.
The reason why what I had originally didn't work because, as Lee Burrows mentioned, the Export for Actionscript hangs the loading if X = 1 in Export on Frame X, regardless if Export on Frame 1 was checked or not. Changing it to 2 or unchecking Export for Actionscript were the two solutions (except if it isn't exported for actionscript, then its code cant be referenced to).
Preloader starts at about 2% now.

ActionScript Retrieving Width/Height Of Loader Image

i'm trying to access the width and height of an image that is added to the stage via a custom LoadImage class. the trace results are 0 even though the image displays correctly. what is the problem?
//frame script
var image:LoadImage = new LoadImage("myImage.jpeg");
addChild(image);
trace(image.width); //returns 0
//-------------------------
package
{
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
public class LoadImage extends Sprite
{
public function LoadImage(imageURL:String)
{
//Load Image
var imageLoader:Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageHandler);
imageLoader.load(new URLRequest(imageURL));
}
private function imageHandler(evt:Event):void
{
addChild(evt.target.content);
}
}
}
If you're trying to access it right after you instantiate it, you don't have access to it's properties. You will have to make it event driven:
class LoadImage loads image
frame script listens for a complete event from LoadImage
LoadImage loads the image, once it has it's hands on it it dispatches the event
frame script works with the data
you need to make an event in LoadImage and once done in imageHandler, dispatch that. When you make your new LoadImage, set up the listener
var image:LoadImage = new LoadImage("myImage.jpeg");
image.addEventListener("complete", loadedImage); //or whatever you call the event
function loadedImage(evt:Event) {
addChild( evt.target );
trace(evt.target.content.width); //returns 0
}
you're trying to get the width and height before the image is completely loaded? see this question for more detail.

Flash AS3: Unable to view loaded swf after loading it inside of package

Good morning friendly Flashers ;) So I've been trying since yesterday to just load a SWF file into my main movie. I've done this before just placing code inside a movieClip, but this time I'm working inside of Class files. I have my main class which calls a function inside of my sub class which contains the loader. My problem is that the swf will load (I can tell via traces) but I cannot see the loaded swf :(
Below is the code inside of my sub class
package src.howdinicurtain {
import flash.net.*;
import flash.display.*;
import flash.events.Event;
public class HowdiniFrame extends MovieClip {
//public var splashLoader;
public var introLoader:Loader = new Loader();
public var introContainer:MovieClip;
private var holdX:Number;
private var holdY:Number;
public function HowdiniFrame(url:String, loadX, loadY):void {
holdX = loadX;
holdY = loadY;
this.addChild(introLoader);
//this.addChild(introContainer);
introLoader.load(new URLRequest(url));
introLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,swfLoaded);
}
public function swfLoaded(e:Event):void {
introLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, swfLoaded);
introContainer = introLoader.content as MovieClip;
//introContainer = MovieClip(introLoader.contentLoaderInfo.content);
addChild(introContainer);
introContainer.x = holdX;
introContainer.y = holdY;
trace("holdX = "+holdX);
trace("holdY = "+holdY);
}
}
}
The code above will load the swf file, I can see the swf files trace statements from the start of the animation to the end, but I cannot actually see the swf file inside of the main swf.
Traces:
The SWF file is = intro.swf
Intro Movie Starts :)
contentLoaderInfo event removed
Intro Movie Ends :(
Here is the code in my main class that calls the sub class function that loads the movie:
var introPath:String = xmlOutput.intro;
trace("The SWF file is = "+introPath+"\r"+"\r");
hc = new HowdiniFrame(introPath, 0, 20);
I swear I throw my code into the first frame of a movieClip and it works fine, I see the animation in the loaded SWF play instantly, but when I have my code inside of Class files I cannot see my SWF at all :( thoughts? ideas? Thanks for any tips!
~ Leon
Always treat your children right. Don't forget to add them in everything you do or else you're a bad parent.
What is hc? Is that a MovieClip on the stage? What if you try:
hc.addChild(new HowdiniFrame(introPath, 0, 20));
or if hc is not a clip on the stage
hc = new HowdiniFrame(introPath, 0, 20);
addChild(hc);