13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with movie clips<br />

Loading an external SWF file<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

In ActionScript 3.0, SWF files are loaded using the Loader class. To load an external SWF file, your ActionScript needs<br />

to do four things:<br />

1 Create a new URLRequest object with the url of the file.<br />

2 Create a new Loader object.<br />

3 Call the Loader object’s load() method, passing the URLRequest instance as a parameter.<br />

4 Call the addChild() method on a display object container (such as the main timeline of a Flash docum<strong>en</strong>t) to add<br />

the Loader instance to the display list.<br />

Ultimately, the code looks like this:<br />

var request:URLRequest = new URLRequest("http://www.[yourdomain].com/externalSwf.swf");<br />

var loader:Loader = new Loader()<br />

loader.load(request);<br />

addChild(loader);<br />

This same code can be used to load an external image file such as a JPEG, GIF, or PNG image, by specifying the image<br />

file’s url rather than a SWF file’s url. A SWF file, unlike an image file, may contain ActionScript. Thus, although the<br />

process of loading a SWF file may be id<strong>en</strong>tical to loading an image, wh<strong>en</strong> loading an external SWF file both the SWF<br />

file doing the loading and the SWF file being loaded must reside in the same security sandbox if Flash Player or AIR is<br />

playing the SWF and you plan to use ActionScript to communicate in any way to the external SWF file. Additionally,<br />

if the external SWF file contains classes that share the same namespace as classes in the loading SWF file, you may need<br />

to create a new application domain for the loaded SWF file in order to avoid namespace conflicts. For more<br />

information on security and application domain considerations, see “Working with application domains” on page 147<br />

and “Loading cont<strong>en</strong>t” on page 1048.<br />

Wh<strong>en</strong> the external SWF file is successfully loaded, it can be accessed through the Loader.cont<strong>en</strong>t property. If the<br />

external SWF file is published for ActionScript 3.0, this will be either a movie clip or a sprite, dep<strong>en</strong>ding on which class<br />

it ext<strong>en</strong>ds.<br />

Considerations for loading an older SWF file<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

If the external SWF file has be<strong>en</strong> published with an older version of ActionScript, there are important limitations to<br />

consider. Unlike an ActionScript 3.0 SWF file that runs in AVM2 (ActionScript Virtual Machine 2), a SWF file<br />

published for ActionScript 1.0 or 2.0 runs in AVM1 (ActionScript Virtual Machine 1).<br />

There are important differ<strong>en</strong>ces wh<strong>en</strong> loading an ActionScript 1.0 or 2.0 SWF file into an ActionScript 3.0 SWF file<br />

(compared to loading an ActionScript 3.0 SWF file). Flash Player provides full backward compatibility with previously<br />

published cont<strong>en</strong>t. Any cont<strong>en</strong>t that runs in previous versions of Flash Player runs in Flash Player versions that<br />

support ActionScript 3.0. However, the following limitations apply:<br />

ActionScript 3.0 code can load a SWF file writt<strong>en</strong> in ActionScript 1.0 or 2.0. Wh<strong>en</strong> an ActionScript 1.0 or 2.0 SWF<br />

file is successfully loaded, the loaded object (the Loader.cont<strong>en</strong>t property) is an AVM1Movie object. An<br />

AVM1Movie instance is not the same as a MovieClip instance. It is a display object, but unlike a movie clip, it does<br />

not include timeline-related methods or properties. The par<strong>en</strong>t AVM2 SWF file cannot access the properties,<br />

methods, or objects of the loaded AVM1Movie object.<br />

Last updated 6/6/2012<br />

329

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!