嵌入 SWF 文件与嵌入图像几乎是相同的。 差别在于您可以将嵌入的 SWF 文件的实例当作 MovieClip 类的实例处理。 (它们实际上是 MovieClipAsset 类的子类, MovieClipAsset 类是 MovieClip 类的子类。)
注意: 您无法直接访问嵌入的 SWF 文件的属性或方法。 但是, 您可以使用 LocalConnection 以允许它们进行通信。
例子1:
- <? xml version = "1.0" encoding = "utf-8" ?>
- < mx:Application
- xmlns:mx = "http://www.adobe.com/2006/mxml"
- viewSourceURL = "src/EmbeddingSwfFiles/index.html"
- layout = "horizontal" width = "290" height = "290"
- horizontalAlign = "center" verticalAlign = "middle"
- >
- < mx:Script >
- <![CDATA[
- [Embed(source="assets/hourglass.swf")]
- [Bindable]
- public var Hourglass:Class;
- ]]>
- </ mx:Script >
- < mx:Image id = "hourglass" source = "{Hourglass}" />
- </ mx:Application >
嵌入 SWF 库资源
您可以在应用程序中嵌入来自现有 SWF 库中的特定符号。 Flash 定义三种类型的符号: Button、MovieClip 和 Graphic。 您可以在 Flex 应用程序中嵌入 Button 和 MovieClip 符号, 但您不能嵌入 Graphic 符号, 因为无法为 ActionScript 导出 Graphic 符号。
此示例使用 [Embed] 元数据标签的 source 属性来指定包含您的库的 SWF 文件, 并使用 [Embed] 元数据标签的 symbol 属性来指定您要在该库中嵌入的符号的链接 ID。
例子2:
- <? xml version = "1.0" encoding = "utf-8" ?>
- < mx:Application
- xmlns:mx = "http://www.adobe.com/2006/mxml"
- viewSourceURL = "src/EmbeddingSwfLibraryAssets/index.html"
- layout = "horizontal" width = "450" height = "240"
- horizontalAlign = "center" verticalAlign = "bottom"
- >
- < mx:Script >
- <![CDATA[
- [Embed(source="assets/library.swf", symbol="BadApple")]
- [Bindable]
- public var BadApple:Class;
- [Embed(source="assets/library.swf", symbol="Pumpkin")]
- [Bindable]
- public var Pumpkin:Class;
- ]]>
- </ mx:Script >
- < mx:Image id = "badApple" source = "{BadApple}" width = "150" height = "151.8" />
- < mx:Image id = "pumpkin" source = "{Pumpkin}" width = "150" height = "131.7" />
- </ mx:Application >
例子3:
下面的例子使用3个不同的标识符加载“assets/icons.swf”文件。每个资源都被赋予了不用的变量。这样即使使用同一个SWF文件,它们也能很容易的引用独立的资源。
- <? xml version = "1.0" encoding = "utf-8" ?>
- <!-- http://blog.flexexamples.com/2007/07/25/embedding-assets-from-swf-files/ -->
- < mx:Application xmlns:mx = "http://www.adobe.com/2006/mxml" layout = "vertical" verticalAlign = "middle" backgroundColor = "white" >
- < mx:Script >
- <![CDATA[
- [Bindable]
- [Embed('assets/icons.swf', symbol='bulletCheck')]
- private static var BULLET_CHECK:Class;
- [Bindable]
- [Embed('assets/icons.swf', symbol='bulletCritical')]
- private static var BULLET_CRITICAL:Class;
- [Bindable]
- [Embed('assets/icons.swf', symbol='bulletWarning')]
- private static var BULLET_WARNING:Class;
- ]]>
- </ mx:Script >
- < mx:Button id = "checkBtn" label = "Check" icon = "{BULLET_CHECK}" />
- < mx:Button id = "warningBtn" label = "Warning" icon = "{BULLET_WARNING}" />
- < mx:Button id = "critcalBtn" label = "Critical" icon = "{BULLET_CRITICAL}" />
- </ mx:Application >
你也可以重构一下代码,使它看起来跟优雅。把Embed标签和各种变量移到一个独立的ActionScript文件中,就像下面这样。
Images.as:
- package
- {
- public class Images
- {
- [Embed('assets/icons.swf' , symbol= 'bulletCheck' )]
- public static const BULLET_CHECK:Class;
- [Embed('assets/icons.swf' , symbol= 'bulletCritical' )]
- public static const BULLET_CRITICAL:Class;
- [Embed('assets/icons.swf' , symbol= 'bulletWarning' )]
- public static const BULLET_WARNING:Class;
- }
- }
- <? xml version = "1.0" encoding = "utf-8" ?>
- <!-- http://blog.flexexamples.com/2007/07/25/embedding-assets-from-swf-files/ -->
- < mx:Application xmlns:mx = "http://www.adobe.com/2006/mxml" layout = "vertical" verticalAlign = "middle" backgroundColor = "white" >
- < mx:Button id = "checkBtn" label = "Check" icon = "{Images.BULLET_CHECK}" />
- < mx:Button id = "warningBtn" label = "Warning" icon = "{Images.BULLET_WARNING}" />
- < mx:Button id = "critcalBtn" label = "Critical" icon = "{Images.BULLET_CRITICAL}" />
- </ mx:Application >