Mar
07
2011[AS3] Creating Preloader for External swf in Flash ActionScript3
Tags: Flash AS3When we load external swf files in Flash Movies, they may take some time to load and it’s better to place a preloader in the main movie to inform user that content is loading. It’s a bit tricky to set up preloader for external swf using ActionScript3 in Flash.
Following example will show how you can load external swf file, create a preloader and show it before displaying the loaded swf file to the user:
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"); //remove the preloader from container clip vBox.removeChild(preLoader); // add the loaded swf to container clip vBox.addChild(swfLoader); currentSWF = MovieClip(swfLoader.content); currentSWF.gotoAndPlay(1); currentSWF.addEventListener(Event.ENTER_FRAME , checkLastFrame); function checkLastFrame(e:Event):void { if (currentSWF.currentFrame == currentSWF.totalFrames) { currentSWF.stop(); // trace("stopped"); } } } var preLoader:loader = new loader(); //position the loading bar preLoader.x = 155; preLoader.y = 185; 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 ); } } //create the container clip var container:MovieClip = new MovieClip(); //define the swf file to load var swfFile:String = 'external-file.swf'; //define a MovieClip to store current swf ref var currentSWF:MovieClip = new MovieClip(); //call the function to load swf, provide container clip and external file launchSWF(container, swfFile); //put it on stage addChild(container);
You can download this functional external swf preloader fla file from here.
Hope that helps.
Cheers!
Related posts:
- [AS3] Loading External SWF into MovieClip using Loader Class in Flash ActionScript3
- Multiple External SWF Slideshow – Loading & Playing Mupltiple External SWFs in AS3 Flash Movie
- [AS3] Load External swf into Main Flash Movie with Play, Pause, Forward, Rewind, Load and Unload Buttons
- External SWF Player with Play, Pause, Forward, Rewind Buttons and Seek Bar
- [AS3] Load External swf into MovieClip & Play/Pause/Forward/Rewind and Stop it at LastFrame from Main Flash Movie
Andre
I really appreciate the post and being able to find you – kudos on the SEO.
I remember searching fro as3 preloaders 2 years ago and finding dozens, Now It seems harder than ever. I find more as2 post than as3. Seems to me more designers are focus on JS libraries than flash.
Ali Qureshi
Hi Andre,
Being an SEO myself, it helps in tutorial copywriting on such topic.
I’m glad I did well on both counts, SEO & Flash.
jefferis peterson
Hi, I am trying out this nice preloader (THANK YOU! Every since AS2, Adobe has made basic functionality needlessly complex!).
It is loading the swf but giving this error:
swf file loaded
TypeError: Error #1034: Type Coercion failed: cannot convert UMEContactCN@538680b1 to flash.display.MovieClip.
at Function/()
It appears to be working, just not sure what this might mean..
Ali Qureshi
Hi jefferis,
You are attempting to load an AS2 compiled swf into AS3 swf. This won’t work, try only AS3 movie.
You are right about Adobe making things needlessly complex. Flash 5 was so simple!
On another note, once you get hold of AS3(it takes some time), it becomes very logical and much easier than AS2.
Cheers.
jefferis peterson
It is an AS3 compiled swf but with a Flash 9 player… and it is working… http://unitedminingequipment.com/News_cnf.php
It is a large swf because it has to embed chinese character sets to display the XML file properly…
Jeff
Ali Qureshi
I just checked the url, and it appears your slideshow is loading just fine, although i do get the error after downloading the file and running locally.
Derek
Hi, I can’t seem to download this file. could you check the link or email it to me please. I’m looking for a AS3 preloader that will load the data of slideshow pro component inside a swf container.
Regards
Ali Qureshi
Hi Derek,
Link is fine. try again,
Derek
Cool, Thanks.
saurav
Hi friend,
Thanks to you for the preloader, I am going to use it in my game. I am facing one problem, that is, I have the preloader in the document size, I want to make it in the size of my staze of the document, like 800x580px.
Please tell me where to change this.
Ali Qureshi
You can easily change the size of the preloader movie clip or better still change the width and height of nested logo graphic symbol inside preloader movie clip.
AtA
Hello, really nice tutorial
I’ve just have a little problem, the video loaded begin before the end of the load. There is the loading bar in progression but I can hear the video sound begin and when the load finish, the video don’t start from the begining ^^’
Here’s my link to see what I’m talking about ^^
http://www.newgrounds.com/portal/view/582057
Ali Qureshi
Please check this similar blog post, It uses the same preloader logic and loads external swf to play from a specific frame.
AtA
Thanks to answer but I’ve try it and that does the same ^^’ The video begin before the end of the load :S
Ali Qureshi
Have you tried it with some other swf file? May be the issue with within your swf file.
AtA
I’ve try with several swf files ^^’ Maybe I have to change something about the weight in the code ? Video size is about 70 to 100 MB.
Ali Qureshi
larger swf file should not be a problem. try this Multiple SWF Slideshow fla file from this blog post
I have checked it, the swf file does not play until it’s fully loaded.
Stewart
Thanks for providing this – it’s been really helpful. However. My flash project gets a value from a php/mysql query. Basically – I pull a competition code from a database, and based on what that code is, I reveal a specific prize in my flash file. The problem with using this preloader is I think I’m loosing that value.
how would I pass on that value from the pre-loader to the .swf being loaded?
this code gets the value passed to the preloader:
var keyStr:String;
var valueStr:String;
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
for (keyStr in paramObj) {
valueStr = String(paramObj[keyStr]); // holds the name of the prize image
}
so how does valueStr get to my .swf file?
thanks.
Stewart
thanks for this post.. but I was wondering – what if you called the pre-loader with the Flashvars parameter set – how would you pass this information on to the SWF you’re loading?
Ali Qureshi
Hi Stewart,
One way of passing the var value to loaded swf is by this (using the above example):
Where the var_value is the loaded value in the main swf movie from the html page via FlashVars.
This is how you get the variable inside swf:
AtA
Thanks I’ll try it
Alex
Hi Ali,
I’ve just tried implementing your code and it is working great to load the external swf. However once the swf is actually loaded none of its functionality works correctly.
I even tried using your template and just dropping in my swf, with the same outcome
Ali Qureshi
Alex, Make sure that you are loading AS3 swf file. AS2 swf cannot be loaded into AS3 compiled movie.
Rodolfo
Hi Ali,
Alex made a very good observation, anytime it load an swf with some functions , the functions dont work.
When it load a simple swf with no functions, no problems… There is something hapening…
thanks for the help!
Ali Qureshi
That might well be the case but whatever I have tested, it worked well for me and I did use some advanced AS3 swfs with lot of functions and code in them. There may be some code issues in those swfs actionscript.
Federico
Ali,
How can I remove the swf loaded? I’m using a buttom but can’t manage how to do this.
Thanks!
Ali Qureshi
This is specific code in flash:
You can check this post to see how it’s done. Demo fla is also available there.
Jozsef
Hi,
Thanks a lot for this code.
I tried to use it and it works but in the way that my swf file I was loading starts and plays till end immediately.
Is there some way to stop it?
Thanks!
Jozsef
I think I got it.
I changed this line:
currentSWF.gotoAndStop(2);
and now it seems it works
Thanks!
anthony
i am VERY new to this, so this is probably a very simple question, but, how do I get this into my .swf file? I have creates a looping .swf slideshow and I want to put this in front, while loading, but not come back up every time the slide show loops back to the beginning as all the other codes I have tried did. Thanks!!
Gianfranco
Hey this is awesome thank you, I got everything to work but I can’t unload it. I have it so that I use the unloadAndStop function on the frames that the external swf shouldn’t appear and it worked before when I used a simple external loader code but with your code it doesn’t want to work.
When I use swfLoader.unloadAndStop(); it tells me “access of undefined property swfLoader.
If I type in container.unloadAndStop(); it doesn’t give me an error but it also doesn’t unload it.
Any help would be appreciated and thank you very much for sharing your code
Ali Qureshi
Please check this post for unloading external swf file. I have shared the code and fla file. swfLoader.unloadAndStop(); should do the job. Just make sure where you have added this code, swfLoader is available there. Defining swfLoader outside the function might help.