Introduction
The Load Movie command is a powerful action
that enables you to break your Flash sites
and projects into discrete, independent
movies. This is advantageous for a few
reasons:
breaking up larger SWFs into separate
movies, and being able to unload those
movies when they are no longer necessary,
means that your site will consume less
RAM and operate more smoothly.
individual files will load more quickly
since they are smaller.
individual movies can be edited and
updated without revising the rest of
the project. In this way multiple developers
may collaborate easily on the same
interface.
Load Movie is even more important because
the Macromedia Flash 4 and 5 creation tools
can not import any Flash 4 or 5-specific
features in SWFs, such as action script,
text fields, and sound. This means that
if you've created an SWF with an audio
track, or one that relies on actionscript,
you will not be able to import those elements
of your movie into Flash. If you want to
use those movies within a larger Flash
project, you must use the Load Movie command.
Load Movie is simple to learn, and once
mastered, it will give your Flash development
a new depth, and complexity . What follows
is a compilation of notes and examples
of the Load Movie command, which I hope
you find helpful.
There are two types of locations into
which one may load an external SWF -- a
level, or an empty movie clip. In general,
I prefer and recommend working with empty
movie clips. Movie clips are more flexible
and agile as you can control positioning;
and by applying motion tweens to the empty
movie, you can animate it at design-time
just like any other symbol occurrence in
Flash. In addition, levels shift when loaded
into larger movies, since the top level
of the top movie is always _level0. Loading
movies into layers is most useful for loading
many external swfs without using the duplicate
movie clip command, or dragging many occurrences
of your empty movie clip onto the stage.
But, in general, I find loading SWFs into
empty movie clips more reliable and useful,
and most of the examples in this tutorial
are based on that method.
As a general guide, I have included the
FLAs for these samples.
This movie automatically loads a separate
text animation movie using an actionscript
on a keyframe. I have also scripted four
buttons to illustrate how to control a
movie once it's loaded. While these actions
are on buttons, they could just as easily
be applied to keyframes.
The basic command is: Load Movie ("Wild
FX_rules.swf", "empty")
This command tells the Flash player to
load the file Wild FX_rules.swf (the text
animation I created) into the movie clip
named "empty".
As I wrote above, once an SWF is loaded
into an empty movie clip, that SWF can
be controlled just like any other movie
clip. So, the movie clip can be stopped,
restarted, reset, unloaded, and reloaded
- all very easily.For example, if you wish
to stop your loaded animation, simply issue
the following command:
Tell Target ("empty")
Stop
End Tell Target
When you want it to restart where it was:
Tell Target ("empty")
Play
End Tell Target
Or, if you wish it to replay from the beginning:
Tell Target ("empty")
GoTo 1 and Play
End Tell Target
Of importance here, is that by telling
an empty movie clip to return to stop or
play or to return to frame 1 and play,
does not necessarily mean that your animation
will do exactly as you intend. For example,
in the attached sample .FLA, the text animation
is a movie clip. In that movie clip, each
letter animation is an independent movie
clip, which means that each letter runs
on its own timeline. So, when you tell "empty" to
stop, for example, you are only instructing
the top timeline to stop - and you'll notice
that no additional letters begin animating.
You will also notice that any letters that
are currently animating will complete their
timeline.
By applying motion tweens to your empty
movie clip, you can animation transitions
of alter position, alpha, color or other
attributes of your external SWFs within
your larger Flash movie. For example, if
you wish to fade your movie out, simply
set two keyframes of the empty movie clip
(with the same instance name at both frames).
The first instance has 100% alpha and the
second instance has 0% alpha. Create a
motion tween between these two keyframes.
Whatever objects are loaded into your empty
movie will fade out when these frames are
played.
The final issue to discuss here is movie
alignment. You will need to align your
SWF with your larger Flash file. There
are ways of making alignment within your
Flash movies easier.
The most important thing to remember when
loading an SWF into an empty movie clip
is that the upper left hand corner of the
SWF you are loading is placed at the center
point of the empty Movie Clip. I have placed
a red circle around the center point of
the empty movie clip in the attached sample
.FLA. One way to simplify matters is to
make your external SWF the same size as
your main movie and place the empty Movie
Clip at 0,0. Alternatively you can make
your external SWF any size you want and
place the empty movie clip where you want
the upper left corner of that movie to
appear.
If you want to set the X- and Y-coordinates
dynamically, you may do so using the "SetProperty" ActionScript
(and the _x and _y properties) to move
the empty movie clip into its proper position
(you may also manipulate the position of
levels with the same command). To find
the current coordinates of a movie clip,
make sure the object inspector is open
(Window -> Inspectors -> Object) and then
select your object on the stage. For the
purposes of this action, make certain that "Use
Center Point" is not checked.
To load multiple movies, you need multiple
places to put them. As I noted above, a
key advantage to loading movies into levels
is that you do not have to create them
manually in the .FLA - the load movie command
itself will create the layer and shift
all other layers down appropriately.
If you wish to load multiple external SWFs
into empty movie clips, those movie clips
either need to be created manually within
Flash, or dynamically with the duplicate
movie command. I will discuss both methods
here.
In either case all you will need is one
empty movie clip in your library. To create
them manually, just drag the movie clip
on the stage (like dragging any symbol
on the stage) and duplicate that layer
as many times as necessary. In this way,
all your empty movies are automatically
aligned.
One NOTE OF CAUTION: Never -- ABSOLUTELY
NEVER -- use the Flash align tool (ctrl-k)
with empty movie clips. While those clips
will appear properly positioned in the
.FLA after being aligned, they will not
render in the .swf. If you need to align
any empty movie clips, manually enter x
and y coordinates in the Object inspector.
Most developers will not need to load more
than one or two SWFs, and manually creating
these empty movies is quite easy. However,
on more complex sites it can become a daunting
task. In such situations, you may wish
to employ the Duplicate Movie Clip command.
Simply create one instance of your empty
movie clip on the stage and give it a name
such as "empty".
Then, to load "my.swf" into your movie,
simply tell the Flash player to:
Duplicate Movie
Clip ("empty", "empty_my", 2)
Load Move ("my.swf", "empty_my")
That's it. What you did with the first
command is to duplicate your empty movie
clip labeled "empty", name it "empty_my" and
give it a depth of 2 (above 1, below 3).
Then, in the next line, you told the Flash
player to load the desired SWF into the
new empty movie clip.
It is possible to create highly complex
interfaces by selectively loading and unloading
movies. These instructions are all you
need for the basics of loading external
SWFs.
Often developers find they are not able
to communicate adequately with and control
external SWFs once they are loaded into
a movie. Wildform has received a number
of questions on this subject, and I will
attempt to resolve some of them here.
Let's begin with a simple example: you
have created a movie which loads an external
SWF, and you do not wish your movie to
begin playing until the external SWF is
loaded. In this case, you can rely on the
fact that the width and height of an empty
movie is 0, and the height and width of
the same movie clip once the external swf
has been loaded is > 0.
So:
if GetProperty("empty_movie",_width) > 0
then, your movie has loaded.
Please note that this will detect when
an external movie has loaded only once
any content appears, since the width of
an empty keyframe, or a keyframe with actionscript,
is 0. If you wish to ensure that more than
one frame of the external SWF has loaded
prior to playing your movie you must know
how many frames you wish to be loaded prior
to playing the movie. With that information,
you may use the GetProperty command to
run your detection. For example, let's
say you wish 42 frames of your external
SWF to load before playing:
If GetProperty("empty",
_framesloaded >= 42)
GoTo ("movie_begins_here") and Play
Else
GoTo PreviousFrame
End If
Now, let's get a little more complex. Let's
say that you have three separate external
SWFs, which you load at run-time, and you
do not wish your movie to begin playing
until all three are loaded. Building on
the concepts and commands from above, you
would want an .FLA with:
- a main timeline with 3 frames and 4 layers
-- one for actionscript, and one each for
each empty movie, and three frames;
- the empty movie clips are far off the
stage in the first two frames, so that
they are present in the SWF, but invisible
to the user;
- the empty movie clips are placed in their
proper positions (wherever you wish them
to animate) in the third frame.
Frame 1 actionscript:
if GetProperty("empty_movie",_width) > 0
if GetProperty("empty_movie2",_width) > 0
if GetProperty("empty_movie3",_width) > 0
Tell Target (empty_movie)
GoTo 1 and play
End Tell Target
Tell Target (empty_movie2)
GoTo 1 and play
End Tell Target
Tell Target (empty_movie3)
GoTo 1 and play
End Tell Target
GoTo Frame 3 and Play
End If
End If
End If
Frame 2 actionscript:
GoTo 1 and Play
Frame 3 actionscript:
Stop
In this movie, while all empty movie clips
are hidden, the Flash player checks if
the first movie is loaded. If it is, it
checks if the second movie is loaded. If
it is, it sees if the third movie is loaded.
If all three are loaded, the movie resets
all three external SWFs to frame 1 (the
beginning) and then skips to frame three
of the main timeline where all the animations
are positioned properly and playing from
the beginning. If any of the three movies
are not loaded, the movie will simply go
on to frame 2, from where it once again
returns to frame 3 - running a loop until
all three movies are loaded.
Please note that this technique tells you
when the first frame of all three movies
is loaded -- but remember that the movies
are not completely loaded. If you want
techniques to determine when the ENTIRE
text animation is loaded, you can use the
GetProperty command along with the _framesloaded
property, as described above. (If GetProperty("empty",
_framesloaded >= 42)).
Now let's say that you have two separate,
external SWFs which you wish to run consecutively
- play the second after the first has completed.
Again, Flash has no built-in mechanism
for this, but assuming you know the number
of frames in your external SWFs, this should
be no problem.
For example, if you know that your one.swf
text animation is 24 frames long, then
you can write code that says something
like
If GetProperty("One",_currentframe) >=
24
Tell Target ("One")
goto 1 and stop
End Tell Target
Tell Target ("Two")
goto 1 and play
End Tell Target
End If
One other way to accomplish this is to
insert set variable commands into your
external SWF. This must be done within
Macromedia Flash. In the last frame of
your external movie, you can add a command
such as:
set variable trigger = "loaded"
Back in frame one of the main timeline,
from your larger movie in Flash try:
If (one:trigger
eq "loaded")
Tell Target ("One")
goto 1 and stop
End Tell Target
Tell Target ("Two")
goto 1 and play
End Tell Target
End If
Now that we've covered a lot of material
on the use and power of Load Movie, I will
spend a few paragraphs on notes concerning
other issues that may arise.
Let's start with the following example.
You have the following files:
Now, let's say Parent.swf contains an empty
movie entitled "load_child" and the action
Load Movie ("child\child.swf", "_level0/load_child").
At runtime, Parent.swf should load Child.swf
without a problem.
Now, let's say that Child.swf, in turn,
loads a new movie:
C:\Wild FXdemo\child\grandchild\Grandchild.swf
So Child.swf contains an empty movie entitled "load_grandchild" and
the action Load Movie ("grandchild\grandchild.swf", "_level0/load_grandchild").
This too, should run fine - at runtime,
Child.swf should load Grandchild.swf.
However, if, under these new circumstances,
you run Parent.swf, which loads child.swf,
you will not be able to load grandchild.swf
for two important reasons.
First, when you load a movie into a new
movie, like we loaded child.swf into parent.swf,
all references in child.swf to _level0,
which had previously referred to the top
level of Child.swf, now refer to the top
level of Parent.swf. So, when you run Parent.swf,
which loads Child.swf, Child.swf attempts
to load Grandchild.swf into a movie called "load_grandchild" at
_level0. Since _level0 now refers to the
top level of Parent.swf, and there is no
movie by that name on that level, the operation
fails.
The other issue preventing successful execution
of this SWF is the directory structure.
When you load child.swf into parent.swf,
all Load Movie actions with relative links
(using "child/child.swf" instead of "c:\Wild
FXdemo\child\child.swf") then take the
base of the top level movie -- in this
instance Parent.swf. So, for example, when
Child.swf, from within Parent.swf, attempts
to load "grandchild\grandchild.swf", Flash
attempts to search for:
Of course this action will fail since the
file it is looking for does not exist.
In the event that a file with the name
that Flash was looking for did exist, then
Flash would load the incorrect movie and
not produce an output error, meaning that
you might not even realize it. This could
cause severe debugging headaches, so be
careful.
The final issue I will discuss regarding
the use of Load Movie, is the shift in
levels when external SWFs are loaded. To
communicate between these movies, you need
to understand the structure that is created
by the load movie command. What you have
at the end of this example, when it works,
is an .SWF with the following structure:
For example, there is a dialog box in parent.swf
(labeled "dialog"), you wish to turn on
from a button on the top level of grandchild.swf.
There are two ways to do this. First, the
button would contain action script telling
dialog to turn on.
You can use an absolute reference to dialog:
On (Press) Begin
Tell Target ("_level0/dialog")
Goto "On" and Stop
End Tell Target End On
Alternatively, your tell target could also
contain a relative reference to dialog:
Begin Tell Target
("../../dialog")
It is up to your individual circumstances
whether it is better to use absolute or
relative linking. Each has advantages for
different purposes, but you should always
be aware of these issues when developing
your structures.