|
||||||
|
||||||
Flash Tutorial - Communicating with Externally Loaded Movies
154 Intermediate
Flash Compatibility: MX
2004 (Also works in Flash MX / Flash
5)
Written by: Rabid Lemming
Editor: Phil Schulz
Length: 11,594 words
Assumed Knowledge: Good knowledge of ActionScript and Flash
Level: Easy but a lot to cover
Access: Free
Note: This tutorial has not yet been edited - so that I cannot be sure it is error free or that the instruction are easy to follow. It will be edited soon. Thanks - Phil.
Cross Ref: There are many other Flash Pre-Loader tutorials including several which cover loading external Flash Movies and loading multiple external Flash Movies. To ensure that you are following the right tutorial for your needs, you may wish to read a brief description of each: Preloader Tutorial Listings
Step one: Communicating between the main Timeline and externally loaded swf Movies
Understanding _lockroot method basic ( I recommend you read the advanced explanation too) Skip this if you only have Flash MX as this only works in Flash MX 2004 Flash Player 7 and above
When you load one Flash swf Movie into another the first issue is whenever you refer to anything weather it be a text box, button, Component or whatever. You have to tell Flash wear the thing you want to refer to is located. Now normally this isn't a problem as you just user the target path for locating the object you want to refer to. However when loading one Flash swf Movie into another it gets a little more complex but quite simple once you get to grasps with it. To understand it properly and how it works we will use an example to help:
Now when you test this Movie it works fine and you should get something like this:
However if you load this Movie in as an external swf Movie it won't work!
Why you may ask. Well because in the code it uses _root witch refers to the
absolute default level of the Movie. As it has been external loaded in as an
external swf it is trying to refer to the base level of the main swf and not
the base level of the swf file that has been loaded in. So basically it’s
looking at the Timeline of the main swf and not the Timeline of the external
swf. To fix this add:
this._lockroot = true;
To the swf Movie been loaded in or add:
MyLoader.content._lockroot = true;
(MyLoader been instance name of the loader Component) to the Timeline of the
Flash Movie our loading the swf into. If you are loading the swf into a Movie
Clip use:
myMovieClipInstanceName._lockroot = true;
myMovieClipInstanceName being the instance name of the Movie Clip that your
loading the external swf into.
As you can see here in this example it uses the same code and although the Movie is been loaded in externally the code still works !
So by setting _lockroot = true; you are telling Flash that whenever you use the _root. method for referring to something in an externally loaded swf Movie you want Flash to refer to the external swf Movie Timeline and not the Timeline of the main Movie as it would do if _lockroot = false; this will only work with _root. So you could have _root.MyMovieClipInstanceName to refer to a Movie Clip in the externally loaded swf Movie and it would work so long as _lockroot was set to true. However if you used: this. or just MyMovieClipInstanceName or even _level0. to refer to the same Movie Clip you will find it probably won’t work
Understanding _lockroot method advanced
When you load certain version 2 Components into a SWF file or into the Loader Component, the Components may not work correctly. These Components include the following: Alert, ComboBox, DateField, Menu, MenuBar, and Window. Use the _lockroot property when calling loadMovie() or loading into the Loader Component. If you’re using the Loader Component, add the following code:
myLoaderComponent.content._lockroot = true;If you’re using a Movie Clip with a call to loadMovie() , add the following code:
myMovieClip._lockroot = true;If you don’t set _lockroot to true in the loader Movie Clip, the loader only has access to its own Library, but not the Library in the loaded Movie Clip.
The _lockroot property is supported by Flash Player 7. It specifies what _root refers to when a SWF file is loaded into a Movie Clip. The _lockroot property is undefined by default. You can set this property within the SWF file that is being loaded or in the handler that is loading the Movie Clip. For example, suppose you have a document called Games.fla that lets a user choose a game to play, and loads the game (for example, Chess.swf) into the game_mc Movie Clip. You want to make sure that, if _root is used in Chess.swf, it still refers to _root in Chess.swf after being loaded into Games.swf. If you have access to Chess.fla and publish it to Flash Player 7 or later, you can add this statement to Chess.fla on the main Timeline:
this._lockroot = true;If you don’t have access to Chess.fla (for example, if you are loading Chess.swf from someone else’s site into chess_mc ), you can set its _lockroot property when you load it, as shown below. In this case, Chess.swf can be published for any version of Flash Player, as long as Games.swf is published for Flash Player 7 or later. Place the following ActionScript on the main Timeline:
chess_mc._lockroot = true;When calling loadMovie() , set the MovieClip._lockroot property to true in the loader Movie, as shown in the following code. If you don’t set _lockroot to true in the loader Movie, the loader has access only to its own Library, but not the Library in the loaded Movie:
myMovieClip._lockroot = true;Step two: Communicating between the main Movie and eternally loaded swf Movie
If you don't have Flash MX 2004 or you don't understand the _lockroot method then the best method for referring to anything in the external swf Movie is by using the level it's on and the full target path. Here are a few tricks on how to find out what the target path is for any Movie Clip, button, Component or etc that’s in an externally loaded swf Movie. Once you know what the target path is for an object in an external swf Movie you then know how to refer to it and control it
When you have created your main Movie and you have it loaded in as an external swf Movie. In the Movie that you use to load your main Movie into with. That’s the Movie you load your external swf into. Do the following in that Flash move:
Other methods of getting the target path:
Method one
If you use: trace(TheInstanceNameOfTheThingYouWant.getInstanceAtDepth()); This should produce an out put message of the instance name of that object and its full target path. Example:
this.createEmptyMovieClip("logo_mc",
1);
trace(this.getInstanceAtDepth(1));
Should give a message in the out put window of: _level0.logo_mc
Be sure that in the publish settings you select the Omit Trace actions option
Method two
If you add a selectable dynamic text box or input box to the Stage and give it the same instance name and var name in the Property Inspector like myTextBox. If you then place that text box in the Movie Clip or external swf or Component or in the same location as the object you want to get the target path for or refer to. When you test you Movie the text box text will automatically display the full target path of wear ever it is located. You can then use that target path from the text box to then refer to the object you want so long as you take away the text box name from the end of the text that you copy from the text box when testing. For example if when you tested your Movie the text box displayed the following:
_level0.textfiles.scrollpaneContact.tmp_mc.form.myTextBox;
Then the location and target path for say a button that you wanted to refer to that was in the same location as the text box would be:
_level0.textfiles.scrollpaneContact.tmp_mc.form.MyButtonInstanceName;
As you can see by copying and pasting the text from the text box when testing and then removing the text box instance name you can add the instance name of the actual object you waned to refer to or get the location of
Understanding the basics of level's and layers:
Ok if you are confused lets try and explain a little more what’s going on. Think of you Movie like a sandwich. You have your bottom level and in that level you have layers. The default level is _level0 witch is always the base layer of the main Movie. So if you use _level0 in your externally loaded swf Movie it will be referring to the main Timeline of the Movie that has loaded the external swf Movie into. And not the Timeline of the externally loaded swf. The same is true for _root. (_level0 and _root are identical and their is no difference between them so use witch ever you like but I personally use _root most of the time when I am not loading one swf into another. If I do have an externally loaded swf been loaded into my main Movie then I tend to use _level and full target path method instead unless I have set lockroot to true in witch case its sometimes ok to use _root. See above on the lockroot method for an explanation on what lockroot does). When you open your Flash Movie you are automatically in level0 and in that level you have your layers. Now as you will know with layers you can order witch objects appear on top of one and another. the same is true with layers as the default level is 0 you can create or put object in levels above or below level 0 to make the appear above or below (behind) level 0. See the tutorial here on understanding levels and layers:
Cross Ref: FS Commands enables the Flash Movie to send JavaScript Commands to the web page or the Browser: FS Commands
Layers are like this:
Level 0 (default level. _root will always refer to this level unless lockroot is used)
Ok now you can load an external swf Movie into your main Movie and you now know how to refer to objects in either Movie. Here are some examples for you to use.
This example shows how to communicated between the main Movie and the externally loaded Movie
Example: Download
the Flash file Free here Will open
and work in Flash MX and Flash MX 2004
This example shows how to control an externally loaded Movie Clip that has been loaded into a level
Example: Download
the Flash file Free here Will open
and work in Flash MX and Flash MX 2004
FAQ for loading external swf Movies :
Some people want to load all their external swf Movies in at the same time at the beginning of their Movie to save loading them again later on. This in my option is draft. As if you are loading them all in at the start then it defeats the purpose of loading them in the first place as external Movies. This is because if you loaded them all in at the start then you may as well include them as part of the main Movie and save your self a lot of time and hassle. It’s ok to load several external Movies in at once but try to avoid it if you can as you’re save your self a lot of problems and hassle. If you really must preload all or some of your external swf Movies in at the same time see below on how to do this. This is based on the very first example at the top of this page but there are some difference in the code. I recommend you download the Flash file though as the explanation is a little long winded but I couldn't help that as no easy way to explain it all.
IMPORTANT NOTE: Name all the external swf Movies like so: 0.swf, 1.swf, 2.swf, 3.swf and etc... (Always start from 0)
Why not try out webwasp's new community. Meet new people, find friends in your area: Webwasp Mates & Dates
|
|
|

Webwasp is Phil Schulz's baby. You are welcome to contact me or become my Facebook friend:
Click here
All material on this site is protected under international copyright © law. DO NOT reproduce any material from this site without written permission. Please ask as permission is often granted.
|