Loading swf files in AS2 used to be a very simple thing, not so more in AS3.
In the old days of AS2, loadMovie(“external-file.swf”, containerClip) would do the job.
Enter AS3, now you first need to define a Loader class and then URLRequest path from where to load the external swf file location.
You also need an EventListener to your Loader object to be used for notifying that the swf has been loaded and is ready.
// This is the Loader instance that will load your SWF. var swfLoader:Loader = new Loader(); // URLRequest points to your external SWF var swfFile:URLRequest = new URLRequest("external-file.swf"); //create the container Movie Clip to load swf var container:MovieClip= new MovieClip(); // Assign an event listener so that Flash informs you when the SWF has been loaded. swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoadedHandler); function swfLoadedHandler(e:Event):void { trace("swf loaded"); } swfLoader.load(swfFile); //just add the loaded swf to container container.addChild(swfLoader); //add container to the stage and make it visible addChild(container);
Accessing Timeline of Externally Loaded SWF file from Main Movie
After your external swf movie has loaded, just store its reference in another movieClip after type casting it as MovieClip, and then you can access its time line easily for stopping, playing, pausing at any frame.
[actionscript3] //store this loaded swf reference in currentSWF MovieClip object to access it later //You need to cast swfLoader.content as a MovieClip before storing its reference var currentSWF:MovieClip= new MovieClip(); currentSWF = MovieClip(swfLoader.content); //and you can easily access loader content's timeline like this currentSWF.gotoAndPlay(10); currentSWF.gotoAndStop(50); </pre>
Hope that helps.
Cheers!
thanks for sharing this method
so
I need to add ANE in main.as
how can i do this
There are several blog posts on this topic. It has been a while that I worked on Flash but please check the posts underneath in related posts section and you will find soem useful post and also the fla samples that I have attached.
Hi, I’m loading a external swf in my fla in as3 but I want to load this swf on all pages in my fla but I can’t pleas help me to do this.
In fact I want to load a contact us page in my fla on the pages and then I want to close this swf and see the pages again could you help me pleas?
Very helpful!!
Thanks for the post.
Favorited 😀
Hi again Ali 🙂
i have this error from another type swf from a similar programm for 3d galleries.
Error #2047: Security sandbox violation
have you an idea about how to fix it?
Are you trying to load external swf from other server? if yes, then you cannot load something from different domain until you have allowed it in crossdomain.xml.
Place this file on the root of the domain from where you are trying to load swf file like this:
Or adjust your security settings to allow your local file to access the internet zone or upload both swfs to same server.
Hi Ali, you mean to save this code in a file as xml with the name crossdomain? and then where exactly on server i have to upload this? (into /httpdocs?)
yes, you need to upload cross-domain.xml to the root of yuor website whatever the folder is, /public_html or /httpdocs
🙂 ok! again thanks for your response! if you have time to look at files from pc some time, you will help, because if there is a solution to it only someone expert to actionscript can help me and all the others who have the same issue!
Thanks for your time!
I have tried this, but it doesn’t work. it seems the external swf have an enter frame event, i don’t know and opens independently. Did you saw my source files? it’s a very small file, can you look at it and try whatever you think? 🙂 i’ll be grateful to you if you help me found a solution to this because i haven’t found something else on the internet and i need it 🙁
offcourse THANKS
If external swf has some enter frame event, it can’t be controlled from main swf. I could not check your file because I’m using phone.
Hi, i have a problem and i cannot find a solution 🙁
can you take a look at this, pleassssse 🙂
I have one swf that was made by Wondershare Flash Gallery Factory Deluxe program, and some other files comes with it.
The problem is that when it loads into my main swf doesn’t appear into the movieclip that i target to the code but on the stage, on top of all others, i don’t know exactly what happens, it seems not to me in the display list.
The point is that i’d like a back button showing in front of the gallery in order to be able to close it and go back.
i’d like you to take a look at this and help me if you know something. all the files is in a zip file, you can download it from here: e-tsantilis.com/actionscriptOrg.zip
You can easily define the level or depth for the container MovieClip. In the above example, change the addChild(container); to addChildAt(container,0); So the last line in the provided fla will look like this:
In a real project, you might want to find out the level dynamically where you want to load your container, not top of all others. For instance you have two clips, one container other box. Put this container clip on top of box clip after finding out the box clip level dynamically and then swap both clips like this:
Cheers!
The code works great! I revised the code a little so that I can load multiple external files to multiple containers and this works great also. I can move the containers around with .x and .y but if I try to change the height and width (.height and .width) the external files do not display. Do you know why this would happen?
The width and height of the loaded SWF is defined by the source .fla can be found in the Loader object in which you’ve loaded the SWF into like this:
This will show you the dimensions as defined in the .fla.
The stage.stageWidth and stage.stageHeight properties will always return the width of the stage, the stage is always the top most SWF or the main swf file where other swfs are loaded. In other words, it will always represent the dimensions of the shell’s stage. There is only one stage in a Flash application.
Ali, i have been trying to get this code working with no success.
This is what i have been using
var swfLoader:Loader = new Loader();
var swfFile:URLRequest = new URLRequest(“preloaderSml.swf”);
var container:MovieClip= new MovieClip();
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoadedHandler);
function swfLoadedHandler(e:Event):void {
trace(“swf loaded”);
}
swfLoader.load(swfFile);
container.addChild(swfLoader);
addChild(container);
var currentSWF:MovieClip = new MovieClip();
currentSWF = MovieClip(swfLoader.content);
i have been trying to call a movieclip from within the swf file called preloader_mc. Code for this below:
currentSWF.preloader_mc.myLoadText.text = percent + “%”; currentSWF.preloader_mc.circleLoader.gotoAndStop(percent);
I keep getting this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at fArtistsGallery_fla::MainTimeline/loadProgress()
I have double and triple checked my instance names in other swf and all is correct
Thanks for any help Ali
Kim
Hi Kim,
You can access the timeline of the loaded swf from your main movie but I’m not really sure if loaded swf’s movieClips could also be accessed. I have never tried that but I think they can’t be accessed.
Thanks for posting this. I got the swf loaded and positioned nicely, but I’m having trouble stopping it at a later frame on the main timeline. The swfLoader loads as part of my home page, frames 1-110, but when a menu button is pressed to move to a later frame,111, the swfLoader continues playing on top. I thought currentSWF.stop(); would execute this but that’s not working.
Any help would be appreciated. Thanks.
Hi Jerome,
All you have to do is to update your function menuClicked1 to this:
You may want to have a look of this Control External SWF with Play/Pause/Forward/Rewind Buttons blog post
Thanks for posting this!
I am very new with actionscript 3.0…
I have set this up to load when I click a button on the stage. It seems to load, as I hear the file I am loading in…however I don’t see it on the screen… any ideas as to what I may have wrong here?
Here is my code
Thanks!
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
//btn_1 START
this.btn_1.addEventListener(MouseEvent.MOUSE_DOWN, myClickHandler);
btn_1.buttonMode = true;
btn_1.useHandCursor = true;
function myClickHandler(myCrazyEvent)
{
// This is the Loader instance that will load your SWF.
var swfLoader:Loader = new Loader();
// URLRequest points to your external SWF
var swfFile:URLRequest = new URLRequest(“videoplayer_design.swf”);
//create the container Movie Clip to load swf
var container:MovieClip= new MovieClip();
// Assign an event listener so that Flash informs you when the SWF has been loaded.
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoadedHandler);
function swfLoadedHandler(e:Event):void {
trace(“swf loaded”);
//store this loaded swf reference in currentSWF MovieClip object to access it later
//You need to cast swfLoader.content as a MovieClip before storing its reference
var currentSWF:MovieClip= new MovieClip();
currentSWF = MovieClip(swfLoader.content)
//and you can easily access loader content’s timeline like this
currentSWF.visible = true;
}
swfLoader.load(swfFile);
//just add the loaded swf to container
container.addChild(swfLoader);
}
//btn_1 END
Hi Tom,
You just forgot to add the container MovieClip to the stage. Just add following line after container.addChild(swfLoader);
Also this line is unncesasry currentSWF.visible = true; in your code and does nothing actually.
Cheers!
No offense meant, Ali- I’m sure if there is an error here, it’s on my part. I’ll have a look at the link.
Thanks!
Thanks for the prompt reply!
So, if I understand correctly, the process to cast the loaded swf is basically the code you have supplied, or is there some other element to casting the content into a variable? I tried to implement the code myself, but it seems to send a ‘no properties’ error.
Code is complete and should work just fine, I did test it before posting it. Also, I have provided a working sample fla file in other similar post, check this post for further explanation and a sample fla file is also posted there.
One caveat here, just make sure you are laoding AS3 compiled swf file not AS2 swf.
Great job on clarifying the loader class, Ali.
Could you explain the process of ‘casting’ the loaded content into a movieclip variable?
Hi Gregory,
You can’t really access the loaded content timeline without first type casting it to MovieClip. Because functionality we need here is provided by the methods gotoAndPlay() and gotoAndStop() etc which are only available to MovieClips.
If you don’t type cast the Loaded swfs, they will start playing or streaming as they are loaded depending upon how they have been coded, whether they are preloaded all before playing or otherwise. Plus you still can’t control the timeline without MovierClip methods anyway.
Cheers!
what a great tutorial, although i have a question… in as3, i have a main swf, and which contains a placeholder movieclip, i want to load a swf into this place holder ( which is no problem) however i want to use a button in the loaded swf to load a new swf in the current placeholder movie clip on the main swf. (and unload/park the original loaded swf).
i have tried using parent.parent, and root i have also read that you can use an event dispatcher/listener in main movie, however, i dont want to have to re-code the main movie every-time i add a new button in the loaded movie to target effectively the same placeholder movie clip, but load a new swf.
Hi Keith,
Have you being abe to sort this out?
i have no idea what i am doing wrong, but when i try to use this potentially-wonderfull code i get the 1009 error message.
here is my code – i ll appriciate if u could take a a look! thanx a lot!
explanations: heart is a library MC, that the external SWF is masked into it..
Hi Yasmin,
Your error seems to be AS3 code trying to typecast AS2 swf file. Make sure you are loading AS3 external swf file and not AS2 compiled swf. Otherwise it will throw that error.
You may want to check this Loading External SWFs post too and the accompanying fla file.
Hope that helps.
Could You give your e-mail? I’d send You my source files, and maybe you’ll figure out something. It’s true that there was something wrong with (i suppose) parent movie clip, i’m just guessing, cause i made a new AS3 project file, and did everything again step by step. My SWF file was explored in its original size, but… it’s still too big, so i’m forced to continue the case.
Yah, send me the files and I’ll have a look. I’ve sent you email.
I have checked your chart swf, and it has been coded very well.
When the chart clips movie loads, it detects the stage width and height of the loading movie and expand to those dimensions. There’s no way you can manipulate that, at least I could not, I even tried to load it in separate container swf file and then loaded the container swf in the main swf but to no avail.
If you set the canvas size of the main file smaller, chart also scales itself down but the interesting part is that text size remains in the original size without distorting itself. They have done wonderful work in that chart clip and quite a coding.
No, I’d like to manipulate the dimensions of the SWF that i’m loading without scaling (as You mentioned). The container’s dimensions manipulation is another one solution, but i’d like to leave it for the end (in case changing width / height wouldn’t work).
I already tried to do the scaling thing, but something was happening with text (You have noticed that), so it ain’t good for me.
To sum up – i need the code which let me manipulate the width / height of loaded movie. This is my actual script, where i defined only tha position of loaded Movie:
var equalizer:Loader = new Loader();
equalizer.load(new URLRequest(“sector.swf”));
equalizer.x=400;
equalizer.y=400;
addChild(equalizer);
The default dimensions of “sector.swf” is 500×300, but during testing my movie i got the “sector.swf” over-scaled about 4-5 times than its original dimensions. Don’t know why.
When i added these two lines of code:
equalizer.witdh = 50;
equalizer.height = 50;
and this in result explore me nothing… The “sector.swf” won’t show at all. Please help :(.
First of all, your code has a typo as “equalizer.witdh” should be equalizer.width.
And your this observation is very interesting and seems to be the reason.
I suspect, from my experience of Flash, that you have scaled the clip or the parent container clip somewhere. Just make sure by checking all the nested clips from stage that you have not scaled any container clip by using “Transform” tool.
You can Select every container clip one by one and check the transform property panel to see the sizes. All must be 100% for width and height.
You can and should use AS code too, if above method fails, to trace the actual width and height of the container clips lest they are being updated somehwere accidentally by your AS code!
I’m sure that your loaded clip does not get scaled automatically, Flash does not do that on its own, it should stay in its original dimensions!
Update:
Also try following code:
Excellent, but what if I would like to define the loaded SWF dimensions? For example lets just assume that original dimensions of loaded SWF file is 500 x 300. If I load this SWF to other Movie like in the top, its dimensions are completely different, much bigger than it should be. So, how would the code look like? I’ve already found (i thought the solution) the scrip, with ScaleX parameters, but it wasn’t good. My loaded SWF is a pie chart, and each part of it has its own description which is easy to read (font-size). After i used the ScaleX parameter, is has changed the loaded SWF dimensions, but also changed the font-size, and that in result wasn’t the solution. So i ask again, are You able to tell me, how would the code look like where i set the original size of loaded SWF? or i could define its size, but through WIDTH & HEIGHT dimensions, not through ScaleX / ScaleY?
If I have understood you correctly, you want to increase or decrease the visibility area of the loaded swf, without changing its height or width. I think if you just set the width and height of the container clip which loads the external swf into itself, you would be fine.
You can easily set the dimensions of MovieClip container by using this:
Set the container just big enough (or small) to show enough contents. Mind it, if you scale swf larger or smaller, text will distort. So avoid scaling, just manipulate dimensions of container.
Hope that resolves your issue.