외부 내용

응용 프로그램을 여러 개의 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."); 
}

이제 SWF 파일에서 사용 가능한 클래스를 현재 응용 프로그램 도메인에서 getDefinition() 메서드를 호출하여 사용할 수 있습니다. 또한 getDefinitionByName() 메서드를 호출하여 클래스에 액세스할 수도 있습니다. 이 기술은 글꼴 및 대용량 에셋을 한 번만 로드하고 다른 SWF 파일로 에셋을 내보내지 않는 방식으로 대역폭을 절약합니다. 유일한 제한은 loader.swf 파일을 통해 응용 프로그램을 테스트하고 실행해야 한다는 것입니다. 이 파일은 먼저 에셋을 로드한 다음 응용 프로그램을 구성하는 다른 SWF 파일을 로드합니다.