FLASH AS3 ,Loader, UILoader, URLLoader的区别 / 範例

  • 关于FLASH AS3中,Loader,UILoader,URLLoader的区别:
    • Loader:专门用于加载swf或图片等显示对象。
    • URLLoader:专门用于加载xml或txt等文本文件,你也可以用他来加载图片,但是没法显示。
    • UILoader:最试用加载图片对象,与Loader相比,有如下优点:
      • (1)可以实时使用percentLoaded 属性确定已加载内容的多少;使用 complete 事件确定何时完成加载。
      • (2)可以通过指定scaleContent属性(默认为true),来让所加载的对象,自动适应UILoader容器的比例。
        • scaleContent=false: 表示让UILoader容器自动适应对象大小。
        • scaleContent=true:表示让对象自动适应UILoader容器大小。
        • maintainAspectRatio=true 表示按照原始文件的宽高比进行缩放。
        • maintainAspectRatio : Boolean
          获取或设置一个值,该值指示是要保持原始图像中使用的高宽比,还是要将图像的大小调整为 UILoader 组件的当前宽度和高度。
        • scaleContent : Boolean
          获取或设置一个值,该值指示是否要将图像自动缩放到 UILoader 实例的大小。
  • Loader

  • 下面的代码示例显示了一些常用技巧。第一个示例显示一个格式正确的侦听器语法,它使用 Loader 组件将内容加载到 SWF 文件中。progress 事件在加载内容时开始,而 complete 事件指示加载完成的时间。

AS2

var boxLdr:mx.controls.Loader;
var ldrListener:Object = new Object();

ldrListener.progress = function(evt:Object)
{
	trace("loader loading:" + Math.round(evt.target.percentLoaded) + "%");
};

ldrListener.complete = function(evt:Object) 
{
	trace("loader complete:" + evt.target._name);
};

boxLdr.addEventListener("progress", ldrListener);
boxLdr.addEventListener("complete", ldrListener);

boxLdr.load(http://www.helpexamples.com/flash/images/image1.jpg);
  • 也可以将本节中第一个示例稍微更改一下,即使用 handleEvent 方法,但这种技巧稍稍麻烦一些。Macromedia 不建议采用此技巧,因为必须使用一系列 if..else 语句或一个 switch语句来检测捕获了哪个事件。

AS2

var boxLdr:mx.controls.Loader;
var ldrListener:Object = new Object();

ldrListener.handleEvent = function(evt:Object) 
{
	switch (evt.type) 
	{
		case "progress" :
			trace("loader loading:" + Math.round(evt.target.percentLoaded) + "%");
		break;
		case "complete" :
			trace("loader complete:" + evt.target._name);
		break;
	}
};

boxLdr.addEventListener("progress", ldrListener);
boxLdr.addEventListener("complete", ldrListener);
boxLdr.load(http://www.helpexamples.com/flash/images/image1.jpg);

  • UILoader

    • The URLLoader class downloads data from a URL as text, binary data, or URL-encoded variables. It is useful for downloading text files, XML, or other information to be used in a dynamic, data-driven application.
    • A URLLoader object downloads all of the data from a URL before making it available to code in the applications. It sends out notifications about the progress of the download, which you can monitor through the bytesLoadedand bytesTotal properties, as well as through dispatched events.
    • When loading very large video files, such as FLV's, out-of-memory errors may occur.
    • When you use this class in Flash Player and in AIR application content in security sandboxes other than then application security sandbox, consider the following security model:
      • A SWF file in the local-with-filesystem sandbox may not load data from, or provide data to, a resource that is in the network sandbox.
      • By default, the calling SWF file and the URL you load must be in exactly the same domain. For example, a SWF file at www.adobe.com can load data only from sources that are also at www.adobe.com. To load data from a different domain, place a URL policy file on the server hosting the data.
      • See also

        URLRequest
        URLVariables
        URLStream
        Reading external XML documents
        Loading external data

        AS3

        package {
            import flash.display.Sprite;
            import flash.events.*;
            import flash.net.*;
        
            public class URLLoaderExample extends Sprite {
                public function URLLoaderExample() {
                    var loader:URLLoader = new URLLoader();
                    configureListeners(loader);
        
                    var request:URLRequest = new URLRequest("urlLoaderExample.txt");
                    try {
                        loader.load(request);
                    } catch (error:Error) {
                        trace("Unable to load requested document.");
                    }
                }
        
                private function configureListeners(dispatcher:IEventDispatcher):void {
                    dispatcher.addEventListener(Event.COMPLETE, completeHandler);
                    dispatcher.addEventListener(Event.OPEN, openHandler);
                    dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
                    dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
                    dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
                    dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
                }
        
                private function completeHandler(event:Event):void {
                    var loader:URLLoader = URLLoader(event.target);
                    trace("completeHandler: " + loader.data);
            
                    var vars:URLVariables = new URLVariables(loader.data);
                    trace("The answer is " + vars.answer);
                }
        
                private function openHandler(event:Event):void {
                    trace("openHandler: " + event);
                }
        
                private function progressHandler(event:ProgressEvent):void {
                    trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
                }
        
                private function securityErrorHandler(event:SecurityErrorEvent):void {
                    trace("securityErrorHandler: " + event);
                }
        
                private function httpStatusHandler(event:HTTPStatusEvent):void {
                    trace("httpStatusHandler: " + event);
                }
        
                private function ioErrorHandler(event:IOErrorEvent):void {
                    trace("ioErrorHandler: " + event);
                }
            }
        }
  • URLLoader

The UILoader class makes it possible to set content to load and to then monitor the loading operation at run time. This class also handles the resizing of the loaded content. If loading content from a different domain (sandbox), security implications may mean content properties are inaccessible. Please see the Loader class for more information.

Using ActionScript to set a property of the UILoader class overrides the parameter of the same name that is set in the Property inspector or Component inspector.

This component wraps flash.display.Loader. The Loader class handles all the actual loading; the UILoader just provides a visual display for the Loader object.

Note: When content is being loaded from a different domain or sandbox, the properties of the content may be inaccessible for security reasons. For more information about how domain security affects the load process, see the Loader class.

UILoaderExample.as

This example demonstrates a simple interaction with the UILoader class.

  1. Place an instance of UILoader on the stage and name that instance "uiLoader".
  2. Place an instance of Label above the UILoader instance and name it "uiLoaderLabel".
  3. Save this code as UILoaderExample.as in the same directory as your FLA.
  4. Set the DocumentClass in the FLA to UILoaderExample.

AS2

package
{
    import flash.display.Sprite;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.events.ProgressEvent;

    public class UILoaderExample extends Sprite
    {
        private var sampleImagePath:String = "test.jpg";
        
        public function UILoaderExample() {
            var request:URLRequest = new URLRequest(sampleImagePath);
            uiLoader.scaleContent = false;
            uiLoader.addEventListener(Event.COMPLETE,loadComplete);
            uiLoader.addEventListener(ProgressEvent.PROGRESS,loadProgress);            
            uiLoader.load(request);
        }
        private function loadProgress(e:ProgressEvent):void {
            uiLoaderLabel.text = String(e.target.percentLoaded);
        }
        private function loadComplete(e:Event):void {
            uiLoaderLabel.text = "Load Complete";    
        }
    }
}

Open the Actions panel, select Frame 1 in the main Timeline, and enter the following ActionScript code:

AS2

import fl.containers.UILoader;

var aLoader:UILoader = new UILoader();

aLoader.source = "http://www.flash-mx.com/images/image1.jpg";
aLoader.scaleContent = false;
addChild(aLoader);

aLoader.addEventListener(Event.COMPLETE, completeHandler);

function completeHandler(event:Event) {
    trace("Number of bytes loaded: " + aLoader.bytesLoaded);
}

posted on 2010-10-31 09:57  Morris  阅读(1704)  评论(0编辑  收藏  举报

导航