Recipe 6.7.
Loading and Interacting with External Movies
Problem
You
want to load, and be able to
interact with, an external .swf movie into your own
movie.
Solution
Use the new Loader class to load the
.swf file, and then access the .swf file via the
content property of the Loader instance.
Discussion
Recipe
6.6 demonstrates how to load external images via the
Loader class. Loading external .swf movies uses the
same techniqueby calling the load( ) method on a Loader
instance and passing a URL to a .swf instead of an image,
the .swf is loaded into the movie. If the Loader is
in the main display hierarchy, the .swf also appears
on-screen.
This recipe involves creating two separate
.swf files, ExternalMovie.swf and
LoaderExample.swf. The first movie,
ExternalMovie.swf, will be loaded at runtime into the second
movie, LoaderExample.swf. The code for
ExternalMovie.swf is as follows:
package {
import flash.display.Sprite;
import flash.display.Shape;
public class ExternalMovie extends Sprite {
private var _color:uint = 0x000000;
private var _circle:Shape;
public function ExternalMovie( ) {
updateDisplay( );
}
private function updateDisplay( ):void {
// If the circle hasn't been created yet, create it
// and make it visible by adding it to the display list
if ( _circle == null ) {
_circle = new Shape( );
addChild( _circle );
}
// Clear any previously drawn content and draw
// a new circle with the fill color
_circle.graphics.clear( );
_circle.graphics.beginFill( _color );
_circle.graphics.drawCircle( 100, 100, 40 );
}
// Changes the color of the circle
public function setColor( color:uint ):void {
_color = color;
updateDisplay( );
}
// Gets the current circle color value
public function getColor( ):uint {
return _color;
}
}
}
The code for ExternalMovie.swf is nothing
out of the ordinarya black circle is created when the movie is
executed. The main thing to notice about the code is that there are
two public methods for accessing and modifying the color
of the circle, getColor( ) and setColor( ). Whenever
the setColor( ) method is invoked, the circle is redrawn
with the updated color value.
By declaring these methods as public,
the methods are able to be called from a movie that loads the
ExternalMovie.swf in at runtime. In contrast, the
private updateDisplay( ) method won't be available
to the loading movie. See Recipe
1.13 for more information about the visibility modifiers for
methods.
Now that ExternalMovie.swf is created, a
new .swf needs to be created to load the external movie.
This is done with LoaderExample.swf, which has the following
code:
package {
import flash.display.*;
import flash.net.URLRequest;
import flash.events.Event;
public class LoaderExample extends Sprite {
private var _loader:Loader;
public function LoaderExample( ) {
// Create the Loader and add it to the display list
_loader = new Loader( );
addChild( _loader );
// Add the event handler to interact with the loaded movie
_loader.contentLoaderInfo.addEventListener( Event.INIT, handleInit );
// Load the external movie
_loader.load( new URLRequest( "ExternalMovie.swf" ) );
}
// Event handler called when the externally loaded movie is
// ready to be interacted with
private function handleInit( event:Event ):void {
// Typed as * here because the type is not known at compile-time.
var movie:* = _loader.content;
// Calls a method in the external movie to get data out
// Displays: 0
trace( movie.getColor( ) );
// Calls a method in the external movie to set data.
// Sets the color in the external movie, which draws
// a circle with the new color, in this case red
movie.setColor( 0xFF0000 );
}
}
}
The code for LoaderExample.swf is more
interesting in that it communicates with the loaded movie. There
are two main aspects in the preceding code:
-
Listening for the init event
-
Accessing the loaded movie via the
content property
The init event is fired when the
loaded movie has initialized enough that its methods and properties
are available to be interacted with. The movie can be controlled
only after the init event
has been fired from the loader. Attempting to interact with a
loaded movie before it has initialized will generate runtime
errors.
To control the loaded movie, you'll first need
to get a reference to it. This is done via the content property of the
Loader class. In the preceding code, the loader
variable refers to the Loader that pulled in the external
.swf file, so you can access the movie via
loader.content. If the loader variable weren't
available, the event.target.content path could be used
instead to get to the contents of the Loader. This is
because event.target refers to the instance that generated
the event, which is the same instance that the loader
variable refers to.
The content property is read-only, and
returns an object of type DisplayObject. In the
LoaderExample.swf code, you'll notice that instead of typing
the movie variable as a DisplayObject, the same type as what
the content property returns, the * type was
used. This is necessary because trying to call the getColor( ) or setColor( ) methods on the
movie reference generates compile-time errors if
movie is typed as a DisplayObject.
The movie being loaded,
ExternalMovie.swf, has two public methods available
for interaction. These methods are not part of the
DisplayObject class; therefore, trying to call one of the
methods from a variable of type DisplayObject is an error.
The * type allows you to call any method that you'd like
on the loaded movie. If the method does not exist in the loaded
movie, a ReferenceError is thrown during execution.
The getColor( ) method returns the color
of the circle in ExternalMovie.swf to
LoaderExample.swf. The LoaderExample.swf reports the
color as 0, which is the same as 0x000000, or the color
black. The setColor( ) method allows
LoaderExample.swf to change the color of the circle drawn by
ExternalMovie.swf. In this case, the color of the circle is
set to red, and you can see that the ExternalMovie.swf
updates the display after the new circle color value is set.
|
It is only possible to interact with .swf
files of Version 9 and above using this technique. When loading
Version 8 and below .swf files, this technique won't work
because ActionScript 3.0 code runs independently of ActionScript
1.0 and 2.0. Communication with these .swf files is not
trivial and involves using LocalConnection as a workaround
to send and receive messages. See Chapter 19
for details.
|
|
See Also
Recipes 1.13
and 6.6
|