AS3 加载文件

 import flash.events.*;
 import flash.display.Loader;
 import flash.net.URLRequest;
 import flash.net.URLLoader;
 import flash.net.URLVariables;

要先导入以上包

加载资源方法

URLRequest的资源可以是图片,文本,swf 等

        private var subLoader:Loader;

        private function InitLoder():void

        {

            subLoader = new Loader();

            

            subLoader.load(new URLRequest("Koala.jpg"));

            subLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);

            subLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);

        }

        private function progressHandler(event:ProgressEvent):void

        {

            trace("文件已经加载:"+(event.bytesLoaded/event.bytesTotal*100).toFixed(2)+"%");

        }



        private function completeHandler(event:Event):void

        {



            trace("文件已经加载完成");

            addChild(subLoader);

        }

若加载后需要读取内容,如简单文本或xml,需要使用URLLoader类

文本内容:name=Jack&age=22

XML内容:

<?xml version="1.0" encoding="UTF-8"?>   

<root>   

      <name>Jack</name>   

      <age>11</age>   

</root> 
        public function Test()

        {

            

            var loader:URLLoader = new URLLoader();

            

            loader.load(new URLRequest("words.txt"));

            loader.addEventListener(Event.COMPLETE,loadCompleteHandler);

            InitLoder();



        }

        

        

        private function loadCompleteHandler(event:Event):void

        {

            var parms:URLVariables=new URLVariables(event.currentTarget.data);

            //var parms:XML=new XML(event.currentTarget.data); //xml文件

            trace(parms.name+"  "+parms.age);

        }

虽然2种loader的完成事件参数都是event,但是用Loader类的事件不能得到currentTarget.data,尝试得到会抛出异常

Loader类加载一个flash资源后可以通过contentLoaderInfo.applicationDomain.getDefinition反射得到内部类,从而可以减小主文件的大小,并且多个swf共享资源

例:

import flash.display.Sprite;   

      import flash.display.MovieClip;   

      import flash.display.SimpleButton;   

      import flash.display.Loader;   

      import flash.net.URLRequest;   

      import flash.events.Event;   

       

      public class MainForm extends Sprite{   

             

            private var loader:Loader;   

            private var myMc:MovieClip;   

            private var myBtn:SimpleButton;   

       

            public function MainForm(){   

                  loader = new Loader();   

                  loader.load(new URLRequest("library.swf"));   

                  loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);    

            }   

             

            public function completeHandler(event:Event){   

                  myMc = getLibMc("MyMc");   

                  addChild(myMc);   

                  myBtn = getLibBtn("MyBtn");   

                  addChild(myBtn);   

                  myBtn.x=100;   

                  myBtn.y=100;   

            }   

             

            public function getLibMc(name:String):MovieClip{   

                  var classReference = 

(loader.contentLoaderInfo.applicationDomain.getDefinition(name) as Class);   

                  return ((new (classReference) as MovieClip));   

            }   

       

            public function getLibBtn(name:String):SimpleButton{   

                  var classReference = 

(loader.contentLoaderInfo.applicationDomain.getDefinition(name) as Class);   

                  return ((new (classReference) as SimpleButton));   

            }   

       

      }   

 

你可能感兴趣的:(as3)