4 Replies Latest reply on May 4, 2009 4:57 AM by Rapizoplo

    Problem with multiple swfloaders and one swf file

    Rapizoplo

      Hello!

       

      Is there any way to load one SWF file at runtime with a Loader and then when the load is complete to set it's content as a source property for two or more SWFLoaders ? Because now if I do that the content is shown only on the last SWFLoader, and the others are showing just a blank white space.

       

      Here is the code that I'm using :

       

      _loader = new Loader();
              
      _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete);
      _loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onIOError,false,0,true);
              
      _loader.load(new URLRequest(a url where the swf file is located));

      private function onLoadComplete(event : Event) : void {
           swf1.source = event.currentTarget.content;
           swf2.source = event.currentTarget.content;
      }
        • 1. Re: Problem with multiple swfloaders and one swf file
          ATIF FAROOQ Level 3

          hi,

             you can use swfLoader instead of loader

             <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onComplete(event)">
               <mx:Script>
                    <![CDATA[
          
                          public var loader:SWFLoader=new SWFLoader()
                          
                          public function onComplete(e:Event):void{
          
                              loader.load('test.swf')
                              loader.addEventListener(Event.COMPLETE,swfloaded)
          
                          
                          }
          
                          public function swfloaded(e:Event):void{
                                   l1.source=loader.source
                            l2.source=loader.source
                          }
          
                                 
                      ]]>
           </mx:Script>
          <mx:SWFLoader  id="l1" x="217" y="10" height="262" width="782" >
                
           </mx:SWFLoader>
           <mx:SWFLoader  id="l2" x="1007" y="10" width="277" height="563" >
                
           </mx:SWFLoader>
          </mx:Application>
          
          1 person found this helpful
          • 2. Re: Problem with multiple swfloaders and one swf file
            Rapizoplo Level 1

            Thanks for your reply ATIF FAROOQ!

             

            Maybe I was a little bit unclear in my question about what the situation exactly is.

             

            Your solution will solve my problem with blank SWFLoader components, but the target SWF file will be loaded twice in memory (or as many swf loader components as I have), which is what I do not want to happen, because the SWF file is quite big and I want to load it only once at the beginning of my application and then use it many times. For example I'm loading every picture that way, but the Bitmap object that is loaded have ".bitmapData" property that can be used to generate and use a new Bitmap object without copying the bitmap content twice in memory.

            • 3. Re: Problem with multiple swfloaders and one swf file
              ATIF FAROOQ Level 3

              following class can be helpful in your case:

              package
              {
                  import flash.display.Loader;
                  import flash.display.Sprite;
                  import flash.events.Event;
                  import flash.net.URLLoader;
                  import flash.net.URLLoaderDataFormat;
                  import flash.net.URLRequest;
                  
                  import mx.core.FlexLoader;
                  import mx.core.UIComponent;
              
                  public class superLoader extends URLLoader
                  {
                     
                      private var buffer:Array=new Array()
                      private var ldr:Loader=new Loader()
                      private var comp:UIComponent
                      private var disp:Sprite
                      public function superLoader(request:URLRequest=null)
                      {
                          super(request);
                          
                      }
                      override public function load(request:URLRequest):void{
                          this.dataFormat=URLLoaderDataFormat.BINARY
                          super.load(request)
                          
                      }
              
                      public function get loadedContent():UIComponent{
                          if(this.data!=null){
                              ldr=new Loader()
                              comp=new UIComponent()
                              ldr.loadBytes(this.data)
                              comp.addChild(ldr)
                              return comp
                          }else{
                              throw new Error('Invalid Data')
                          }
                      }
                      private function onLoad(e:Event):void{
                              e.target.removeEventListener(Event.COMPLETE,onLoad)
                              buffer.shift().addChild(e.target.content)
                              e=null
                      }
                      public function get rawLoadedContent():Sprite{
                          if(this.data!=null){
                          ldr=new Loader()
                              disp=new Sprite()
                              ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoad)
                              ldr.loadBytes(this.data)
                              buffer.push(disp)
                              return disp
                          }else{
                              throw new Error('Invalid Data')
                          }
                      }
                      
                  }
              }
              
              • 4. Re: Problem with multiple swfloaders and one swf file
                Rapizoplo Level 1

                Thank you VERY much for your help !

                 

                However I have done a little modification to your code, because even using the "get rawLoadedContent" method, the SWFLoader was showing the swf file with wrong width and height properties. Also this method return a sprite object insted of the original content which in my situation was a problem, because I have to call a few functions that are within the target swf file.

                 

                Here is my final version of the code, in case that someone else is having the same problem as I had :

                 

                package
                {
                    import flash.display.Loader;
                    import flash.display.Sprite;
                    import flash.events.Event;
                    import flash.net.URLLoader;
                    import flash.net.URLLoaderDataFormat;
                    import flash.net.URLRequest;
                    
                    import mx.core.FlexLoader;
                    import mx.core.UIComponent;
                 
                    public class superLoader extends URLLoader
                    {
                       
                        private var buffer:Array=new Array()
                        private var ldr:Loader=new Loader()
                        private var comp:UIComponent
                        private var disp:Sprite
                        public function superLoader(request:URLRequest=null)
                        {
                            super(request);
                            
                        }
                        override public function load(request:URLRequest):void{
                            this.dataFormat=URLLoaderDataFormat.BINARY
                            super.load(request)
                            
                        }
                 
                        public function get loadedContent():UIComponent{
                            if(this.data!=null){
                                ldr=new Loader()
                                comp=new UIComponent()
                                ldr.loadBytes(this.data)
                                comp.addChild(ldr)
                                return comp
                            }else{
                                throw new Error('Invalid Data')
                            }
                        }
                        private function onLoad(e:Event):void{
                                e.target.removeEventListener(Event.COMPLETE,onLoad)
                                var obj : Object = buffer.shift();
                
                                if(obj.assignObject != null && obj.assignObject.hasOwnProperty(obj.assignProp))
                                {
                                    obj.assignObject[obj.assignProp] = e.target.content;
                                }
                                e=null
                        }
                        public function getRawLoadedContent( assignObject : Object, 
                                                             assignProperty : String = "source"):void{
                            if(this.data!=null){
                                ldr=new Loader()
                                
                                var obj : Object = {"assignObject" : assignObject, "assignProp" : assignProperty};
                
                                ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoad)
                                ldr.loadBytes(this.data)
                                buffer.push(obj)
                            }else{
                                throw new Error('Invalid Data')
                            }
                        }
                        
                    }
                }