Skip to main content
Inspiring
January 14, 2009
Question

Using AS3 To Load External SWF Files

  • January 14, 2009
  • 11 replies
  • 4000 views
I'm making the transition to AS3 and can not figure out how to do something that I used to do easily in AS2. I have a "master" flash file (master.swf). I've set up a loader to load a second flash file into it (home.swf). I'm using the attached code on the master.swf.

All good so far. Now I want to create a navigation menu on the "home.swf" so I can add and replace more swfs into the overall layout. It would seem to make the most sense to use the loader I've already created in the "master.swf" to do this. But I can't figure out how to access and use that loader. I tried the attached code, hoping that the loader would replace the "home.swf" located in level 1 with a new swf ("bio.swf"). But it doesn't work and throws an error. I'm guessing that the button I've created in the "home.swf" to load the "bio.swf" doesn't know about the "myLoader" loader I created in the "master.swf."

Any advice or direction would be very welcome. Thank you.
This topic has been closed for replies.

11 replies

kglad
Community Expert
Community Expert
January 30, 2009
var is a keyword so change that to var1 or something other than var and another keyworkd.

you can then use home's loaderInfo.parameters properties:

trace(loaderInfo.parameters.var1); // output = abc
Participating Frequently
January 30, 2009
anyone knows if it is possible to pass parameter to a swf ?

e.g.
var homeRequest:URLRequest = new URLRequest("home.swf?var=abc");

and then how can home.as read var ?

tks
kglad
Community Expert
Community Expert
January 16, 2009
a displayobject is the most basic element that can be displayed on stage. if you draw a shape on-stage, that's a display object. a displayobjectcontainer is an object that can contain a display object. shapes you draw on-stage cannot parent another displayobject so shapes (or graphics) are not displayobjectcontainers. every displayobjectcontainer must be a displayobject plus that one extra ability to contain other display objects. sprites and movieclips are displayobjectcontainers.

for most people with a little experience with flash, understand what constitutes a movieclip. most people know it has a timeline and can contain child objects and, of course, it can be added to the display list. so, a movieclip is a displayobject and a displayobjectcontainer and much more.

the point of mentioning the movieclip class is, if you understand what constitutes a movieclip, you can understand what constitutes a sprite: it's a movieclip with one frame. and you can't add custom properties to it. those are two major differences but otherwise there are no other differences.

the stage is the main drawing region. so, it's a displayobjectcontainer and displayobject.
kglad
Community Expert
Community Expert
January 15, 2009
use the content property of your loader (cast as a movieclip) to reference the main timeline in your loaded swf AFTER loading is complete:

MovieClip(myLoader.content).gotoAndStop(5);
Participant
January 16, 2009
Thanks a ton kglad. It worked.

Now I have one more problem:

I want to remove the child.swf (that I loaded externally into my main.swf) using a button on main.swf. How do I do that. I tried using removeChildAt method, but it gives me this error "ArgumentError: Error #1063: Argument count mismatch on child_fla::MainTimeline/rem(). Expected 0, got 1."
The child.swf has abutton which has the following action added to it"
rem_btn.addEventListener(MouseEvent.CLICK,rem);
function rem() {
stage.removeChildAt(0);
}"

Participant
January 16, 2009
I found the error.....

The function rem() was written incorrectly :)

One more thing...I am little confused between displayObject container, sprite and stage. Though am reading the help file of flash, it still is a little unclear to me. How are they different and which to use when.

Any download would be really great..


quote:

Originally posted by: Abhiuday
Thanks a ton kglad. It worked.

Now I have one more problem:

I want to remove the child.swf (that I loaded externally into my main.swf) using a button on main.swf. How do I do that. I tried using removeChildAt method, but it gives me this error "ArgumentError: Error #1063: Argument count mismatch on child_fla::MainTimeline/rem(). Expected 0, got 1."
The child.swf has abutton which has the following action added to it"
rem_btn.addEventListener(MouseEvent.CLICK,rem);
function rem() {
stage.removeChildAt(0);
}"




kglad
Community Expert
Community Expert
January 14, 2009
you're welcome.
Participant
January 15, 2009
Continuing with backpages issue, can some one guide me as how I an interact with the externally loaded movie.

Essentially, what I have is this:
The code for loading the movie is essentially the same as backpages.

I have a swf called "main.swf" which contains a button called "button1".
In the main.swf I have loaded a swf called "child.swf". This swf has 5 frames

What I need is that on clicking the button1 the child.swf should go to frame 5. I know its very basic and believe me it was so much easier to do in AS2, but with AS3 I am a little confused.

Any suggestion would be very welcome. Thanks.
kglad
Community Expert
Community Expert
January 14, 2009
on the loaded swf's main timeline:

this.parent.removeChild(this);
backpagesAuthor
Inspiring
January 14, 2009
Thank you very much. Hopefully, I should now be able to figure out how to do what I want.
backpagesAuthor
Inspiring
January 14, 2009
This all makes sense to me except for one thing. I have succeeded in creating a scenario where I can easily add and remove various swfs to a master swf by creating buttons on the master swf which remove the loader from the display list. But... what I can't figure out is how to create a button on the swf which has been loaded to remove itself. In other words, if the swf loaded into the master swf is the same size as the master swf then the button to remove that swf and replace it with another swf will have to be part of the second swf. I can't figure out how code something to remove itself. Thanks again for an insight.
backpagesAuthor
Inspiring
January 14, 2009
Thank you very much. I'll see if I can figure this out.
kglad
Community Expert
Community Expert
January 14, 2009
there are no levels in as3. those are depths.

you can't place a display object at depth 1, unless there's something at depth 0. flash forces you to use depths, in order, starting with 0.

each displayobjectcontainer (including movieclips) has its own set of depths (just like as2).

to remove home.swf entirely, you must

1. stop all streams in home.swf (like sounds, videos etc)

2. remove all references to home.swf. ie, remove all references to your loader and remove your loader from the display list

3. null your loader: myLoader=null.
backpagesAuthor
Inspiring
January 14, 2009
Thank you very much for the quick reply. That make sense. I'm now confused about the "levels."

I have my master.swf and I use stage.addChildAt (myLoader,1) to load the home.swf into the master.swf. In the home.swf I am loading several items via ActionScript into it at levels 1, 2 and 3.

So, it seems that each .swf has its own set of levels, correct? And if so, then how do I remove "home.swf" entirely. It seems now when I use stage.removeChildAt(1) while in the home.swf it's just removing the item I've loaded into 1 in the home.swf. How do I remove home.swf completely? I hope this makes sense. Thank you very much.