| Flex 2 Developer's Guide > Flex Programming Topics > Embedding Assets > Embedding asset types > Embedding SWF files | |||
Flex fully supports embedding Flash SWF files. This section describes how to embed different types of SWF files.
You can embed SWF files created for Flash Player 8 and earlier. When embedded, your Flex 2 application cannot interact with the embedded SWF file. That is, you cannot use ActionScript in a Flex 2 application to access the properties or methods of a SWF file created for Flash Player 8 or earlier.
|
NOTE |
|
You can use the flash.net.LocalConnection class to communicate between a Flex 2 application and a SWF file created for Flash Player 8 or earlier. |
For example, you use the [Embed] metadata tag in ActionScript to embed a SWF file, as the following code shows:
[Embed(source="icon.swf")] [Bindable] public var imgCls:Class;
In this example, you define a class named imgCls that represents the embedded SWF file. Flex defines imgCls as a reference to a subclass of the mx.core.MovieClipLoaderAsset class, which is a subclass of the flash.disaply.MovieClip class. Therefore, you can manipulate the image by using the methods and properties of the MovieClipLoaderAsset class.
Flex lets you reference exported symbols in an embedded SWF file. If the symbol has dependencies, Flex embeds them also; otherwise, Flex embeds only the specified symbol from the SWF file. To reference a symbol, you specify the symbol parameter, as follows:
[Embed(source='SWFFileName.swf', symbol='symbolName')]
|
NOTE |
|
Flash defines three types of symbols: Button, MovieClip, and Graphic. You can embed Button and MovieClip symbols in a Flex application, but you cannot embed a Graphic symbol because it cannot be exported for ActionScript. |
This capability is useful when you have a SWF file that contains multiple exported symbols, but you want to load only some of them into your Flex application. Loading only the symbols required by your application makes your resulting Flex SWF file smaller than if you imported the entire SWF file.
A Flex application can import any number of SWF files. However, if two SWF files have the same filename and the exported symbol names are the same, you cannot reference the duplicate symbols, even if the SWF files are in separate directories.
If the SWF file contains any ActionScript code, Flex prints a warning during compilation, and then stripped out the ActionScript from the embed symbol. This means that you can only embed the symbol itself.
The following example imports a green square from a SWF file that contains a library of different shapes:
<?xml version="1.0"?>
<!-- embed\EmbedSWFSymbol.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >
<mx:VBox id="vBox0" width="300" height="300" >
<mx:Image id="image0"
source="@Embed(source='circleSquare.swf', symbol='greenSquare')"
/>
</mx:VBox>
</mx:Application>
If you use the [Embed] metadata tag in ActionScript to embed the symbol, you can access the object that represents the symbol, as the following code shows:
[Embed(source='shapes.swf', symbol='greenSquare')] [Bindable] public var imgCls:Class;
In this example, you define a class named imgCls that represents the embedded symbol. Internally, Flex defines imgCls as a reference to a subclass of either one of the following classes:
SpriteAsset For single-frame SWF files.
MovieClipLoaderAsset For multiframe SWF files.
You can embed SWF files that represent Flex 2 applications. For example, you use the [Embed] metadata tag in ActionScript to embed a SWF file, as the following code shows:
[Embed(source="flex2.swf")] [Bindable] public var flexAppCls:Class;
In this example, you define a class named flexAppCls that represents the embedded SWF file. Flex defines flexAppCls as a reference to a subclass of the mx.core.MovieClipLoaderAsset class, which is a subclass of the flash.disaply.MovieClip class. Therefore, you can manipulate the embedded SWF file by using the methods and properties of the MovieClipLoaderAsset class.
You typically embed a Flex 2 application when you do not require the embedding application to interact with the embedded application. If the embedded application requires interactivity with the embedded application, you might consider implementing it as a custom component, rather than as a separate application.
Alternatively, if you use the SWFLoader control to load the Flex 2 application at run time, the embedding application can interact with the loaded application to access its properties and methods. For more information and examples, see Interacting with a loaded Flex 2 application.
Flex 2.01