---
title: "优化Flash平台的性能――优化网络交互外部内容"
author: "vu060vu"
date: "2012-01-19T13:42:28Z"
tags: []
categories: ["非技术"]
slug: "flash"
draft: false
lastmod: "2012-01-19T13:42:28Z"
id: 1358453
url: "https://vu060vu.iteye.com/blog/1358453"
---

优化Flash平台的性能――优化网络交互外部内容 
2010年07月21日
　　 
　　
　　将应用程序分为多个 SWF 文件。
　　移动设备可能具有受限的网络访问权限。要快速加载内容，可将应用程序分为多个 SWF 文件。尝试重用整个应用程序的代码逻辑和资源。例如，考虑将一个应用程序分为多个 SWF 文件，如下图所示：
　　
　　
　　 分为多个 SWF 文件的应用程序
　　在此示例中，每个 SWF 文件都包含同一位图的其自己的副本。使用运行时共享库可以避免这种重复，如下图所示：
　　
　　
　　 使用运行时共享库
　　借助此技术，可以加载运行时共享库，以使位图对其他 SWF 文件可用。ApplicationDomain 类存储已加载的所有类定义，并通过 getDefinition() 方法使它们在运行时可用。 
　　运行时共享库也可以包含所有代码逻辑。整个应用程序可在运行时更新而无需重新编译。以下代码加载运行时共享库，并在运行时提取 SWF 文件中包含的定义。此技术可用于字体、位图、声音或任何 ActionScript 类：
　　// Create a Loader object var loader:Loader = new Loader();   // Listen to the Event.COMPLETE event loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingComplete );   // Load the SWF file loader.load(new URLRequest("library.swf") ); var classDefinition:String = "Logo";   function loadingComplete(e:Event ):void {     var objectLoaderInfo:LoaderInfo = LoaderInfo ( e.target );          // Get a reference to the loaded SWF file application domain     var appDomain:ApplicationDomain = objectLoaderInfo.applicationDomain;           // Check whether the definition is available     if ( appDomain.hasDefinition(classDefinition) )     {             // Extract definition         var importLogo:Class = Class ( appDomain.getDefinition(classDefinition) );           // Instantiate logo         var instanceLogo:BitmapData = new importLogo(0,0);           // Add it to the display list         addChild ( new Bitmap ( instanceLogo ) );     } else trace ("The class definition " +  classDefinition + " is not available."); }
　　通过在正在加载的 SWF 文件的应用程序域中加载类定义，可以更容易获取定义：
　　// Create a Loader object var loader:Loader = new Loader();   // Listen to the Event.COMPLETE event loader.contentLoaderInfo.addEventListener ( Event.COMPLETE, loadingComplete );   // Load the SWF file loader.load ( new URLRequest ("rsl.swf"), new LoaderContext ( false, ApplicationDomain.currentDomain) ); var classDefinition:String = "Logo";   function loadingComplete ( e:Event ):void {     var objectLoaderInfo:LoaderInfo = LoaderInfo ( e.target );          // Get a reference to the current SWF file application domain     var appDomain:ApplicationDomain = ApplicationDomain.currentDomain;           // Check whether the definition is available     if (appDomain.hasDefinition( classDefinition ) )     {             // Extract definition         var importLogo:Class = Class ( appDomain.getDefinition(classDefinition) );           // Instantiate it         var instanceLogo:BitmapData = new importLogo(0,0);           // Add it to the display list         addChild ( new Bitmap ( instanceLogo ) );     } else trace ("The class definition " +  classDefinition + " is not available."); }
　　现在，通过对当前应用程序域调用 getDefinition() 方法，可以使用加载的 SWF 文件中提供的类。您还可以通过调用 getDefinitionByName() 方法访问这些类。因为此技术仅加载一次字体和大型资源，所以节省了带宽。资源不会以任何其他 SWF 文件的形式导出。唯一的限制是应用程序必须经过测试并通过 loader.swf 文件运行。此文件首先加载资源，然后加载组成该应用程序的不同 SWF 文件。