`
steely816
  • 浏览: 126950 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Event.INIT Event.COMPLETE 区别

阅读更多
   package{  
    import flash.display.*;  
    import flash.events.Event;  
    import flash.net.URLRequest;  
    public class LoadJPG extends Sprite{  
     private var myLoader:Loader;  
     public function LoadJPG(){  
      myLoader=new Loader();  
      myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeListener);  
     myLoader.contentLoaderInfo.addEventListener(Event.INIT,initListener);  
     myLoader.load(new URLRequest("fengjing.jpg"));  
    }//end of constructor function LoadJPG  
    private function completeListener(e:Event):void{  
     trace("This is from Event.COMPLETE listener");  
     trace(myLoader.content.width);  
     trace(myLoader.getChildAt(0).height);  
     trace(e.target.content.rotation);  
     trace("添加到显示列表前 : ",myLoader.numChildren);//1  
     addChild(myLoader.content);  
     trace("添加到显示列表后 : ",myLoader.numChildren);//0  
    }//end of function completeListener  
    private function initListener(e:Event):void{  
     trace("This is from Event.INIT listener");  
     trace(myLoader.content.width);  
     trace(myLoader.getChildAt(0).height);  
     trace(e.target.content.rotation);            
    }//end of function initListener  
   }//end of class LoadJPG   
  }//end of package  
 

例如,在加载影片或动画的第一帧时将调度 Event.INIT。然后,影片将可供访问并可添加到显示列表中。不过,下载完整的影片可能需要花更长的时间。只有在加载了完整的影片之后才会调度 Event.COMPLETE。

init 事件始终在 complete 事件之前。

访问用Loader加载进来的显示资源的时候,通常有3种办法,比如,myLoader是Loader类型的实例,要访问其加载的内容,可以这样:

1,myLoader.content;//直接访问实例的content

2,myLoader.getChildAt(0);//被加载的资源是myLoader的唯一孩子

3,e.target.content;//在侦听函数里面通过事件的目标对象访问

如果要为Loader添加侦听,必须通过Loader的实例变量contentLoaderInfo注册,contentLoaderInfo变量提供了对它装载资源的LoaderInfo对象的一个引用。而不能直接在Loader的实例上添加侦听!

以下代码简单展示了访问装载资源对象的方法和添加侦听的方法,有一个小疑惑啊,事件类型Event.COMPLETE和Event.INIT有什么区别,只知道前者是在加载结束之后触发,后者是在加载初始化后触发。牢记一点Event.COMPLETE永远发生的比Event.INIT晚 ,所以是不是在更多的情况下考虑使用Event.COMPLETE?

参考书对Event.INIT的解释如下

1, 对位图而言,实例化发生于当外部文件已经完全装载的时候。在那个时期,被装载的像素数据自动地被放置在一个BitmapData对象中,然后它被关联于一 个新的Bitmap对象,改Bitmap对象表示被装载的图像。(是不是对位图而言Event.INIT跟Event.COMPLETE是一样的?)

2,对于swf文件,实例化发生于当在帧1的所有资源和类(包括.swf的主类)已经被接收到的时候。在那个时期,ActionScript创建.swf主类的一个实例,并执行它的构造器。该主类实例表示被装载的.swf文件。

注 意,千万不要在初始化完成之前(Event.INIT触发之前)或者加载完成之前(Event.COMPLETE触发之前)试图访问被加载的资源。此时会 报错,如果用myLoader.content访问,content此时为空,如果用myLoader.getChildAt(0)访问则报错,错误 为:RangeError:Error #2006

当被装载的资源对象添加到一个新的DisplayObjectContainer中,被装载的资源自动从它原来的父亲容器Loader对象中移除!

 

分享到:
评论

相关推荐

    UPnP_Programming_Guide

    4.1.13 UPNP E INIT FAILED [-117] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 4.1.14 UPNP E BAD HTTPMSG [-119] . . . . . . . . . . . . . . . . . . . . . . . . . ....

    CS6打开歌词同步频谱播放器源码

    xmlLoader.addEventListener(Event.COMPLETE,xmlLoaderCompleteHandler); } private function xmlLoaderCompleteHandler(event:Event):void { xml=new XML(event.target.data); title=xml.song.@...

    AS3.0歌词同步频谱原码

    lrcLoader.addEventListener(Event.COMPLETE,loadCompleteHandler); stage.addEventListener(Event.ENTER_FRAME,soundPlayingHandler); this.addEventListener(Event.ENTER_FRAME,creatSpectrum); } ...

    tweenjs.min.js文件

    .addEventListener("complete",a.onComplete))}var a=createjs.extend(AbstractTween,createjs.EventDispatcher);a._setPaused=function(a){return createjs.Tween._register(this,a),this},a.setPaused=createjs....

    DebuggingWithGDB 6.8-2008

    Table of Contents Summary of gdb . . . . . . . . ....Free Software ....Free Software Needs Free Documentation ....Contributors to gdb....1 A Sample gdb Session ....2 Getting In and Out of gdb ....2.1 Invoking gdb ....

    python3.6.5参考手册 chm

    Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python ...

    Sakemail

    :)26/3/981.6.1- Added a FUNCFileName private variable to manage the complete path of the attached file. I receive problems reports with this, it work now ?.- Reduced the line sleep to 30 (tell me if ...

    AdminLTE-3.0.2.zip

    Release Notes some small control sidebar fixes fixed content height calculation with overlapping control sidebar fixed collapse/show functions (functions was ...For the complete changelog look here.

    my97日历控件

    J()){_=_||setInterval(function(){if(V[N].readyState=="complete")clearInterval(_);U(null,true)},50);return}if($dp.status==0){$dp.status=1;L.el=T;I(L,true)}else return}else if(L.eCont){L.eCont=$dp.$(L....

    php.ini-development

    ;;;;;;;;... 1.... 2.... 3.... 4.... 5.... 6.... The syntax of the file is extremely simple.... Section headers (e.g.... at runtime.... There is no name validation.... (e.g.... previously set variable or directive (e.g....

    [精典材料]Patran的PCL用户手册.doc

    The init() Function 77 The display() Function 78 Widget Callbacks 81 What Are They? 81 What Are They Used For? 82 Callback Arguments (Widget Function Data) 82 Widget Placement 84 Widget Variables 86 ...

    8-07-14_MegaCLI for linux_windows

    SCGCQ00324525 Defect MegaCLI: MFI_STAT_NOT_FOUND returns when retrieving the FW event log. SCGCQ00325890 Defect CLI/Segmentation fault while running pdlist and cfgdsply in CentOS SCGCQ00326673 Defect ...

    Bochs - The cross platform IA-32 (x86) emulator

    - converted the complete hdimage stuff to the new 'hdimage' plugin - new hdimage method get_capabilities() that can return special flags - vmware3, vmware4 and vvfat classes now return HDIMAGE_HAS_...

Global site tag (gtag.js) - Google Analytics