Flash 平台技术的优化(十六) 将应用程序分为多个 SWF 文件
Flash 平台技术的优化(十六) 将应用程序分为多个 SWF 文件
2010年11月19日
移动设备可能具有受限的网络访问权限。要快速加载内容,可将应用程序分为多个 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.CO MPLETE, 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 文件。
猜你喜欢
转载自dx671dx.iteye.com/blog/1363151
今日推荐
周排行