1 Reply Latest reply on Jan 12, 2011 2:58 AM by Gurdeep Singh

    Flex 4 Custom Preloader not Working

    Gregory Lafrance Level 6

      I am following this help page to create a custom preloader extending Sprite to load an animation SWF, but it is not working (the animation SWF is not displaying):

       

      http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7e3c.html#WS2d b454920e96a9e51e63e3d11c0bf62d75-7feb

       

      I know the Animation.swf file is okay, because if I load it into the main app it displays and runs.

       

      The preloader works if an image is loaded by the preloader instead of the animation SWF.

       

      --------------------- test.mxml (main app) -----------------

      BTW, I typically have many more lines of ComboBox in the app to force the preloader to display, but limiting number of lines here.

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx"
        preloader="customPreloaders.SparkAnimationProgressBar" cachePolicy="off">
        <s:layout>
          <s:VerticalLayout/>
        </s:layout>
        <s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/>
        <s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/>
        <s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/>
        <s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/>
        <s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/>
        <s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/>
        <s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/>
        <s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/>
        <s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/>
        <s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/>
        <s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/>
        <s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/>
        <s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/>
        <s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/>
        <s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/><s:ComboBox/>
      </s:Application>
      

       

      --------- customPreloaders.SparkAnimationProgressBar.as ----------------

      package customPreloaders
      {
          import flash.display.*;
          import flash.events.*;
          import flash.net.*;
          import flash.utils.*;
          
          import mx.events.*;
          import mx.preloaders.*;
          
          public class SparkAnimationProgressBar extends Sprite
            implements IPreloaderDisplay{
            
            // Loader instance to load the animation SWF file.
            private var animationLdr:flash.display.Loader;
            public function SparkAnimationProgressBar() {
              super();
            }
            
            // Add event listeners.
            public function set preloader(preloader:Sprite):void {
              preloader.addEventListener(
                ProgressEvent.PROGRESS, handleProgress);
              preloader.addEventListener(
                Event.COMPLETE, handleComplete);
              preloader.addEventListener(
                FlexEvent.INIT_PROGRESS, handleInitProgress);
              preloader.addEventListener(
                FlexEvent.INIT_COMPLETE, handleInitComplete);
            }
            
            // Initialize the Loader control in the override
            // of IPreloaderDisplay.initialize().
            public function initialize():void {
              animationLdr = new flash.display.Loader();
              animationLdr.contentLoaderInfo.addEventListener(
                Event.COMPLETE, loader_completeHandler);
              animationLdr.load(new URLRequest("assets/Animation.swf"));
            }
            
            // After the SWF file loads, set the size of the Loader control.
            private function loader_completeHandler(event:Event):void
            {
              addChild(animationLdr);
              animationLdr.width = 200;
              animationLdr.height= 200;
              animationLdr.x = 100;
              animationLdr.y = 100;
            }
            
            // Define empty event listeners.
            private function handleProgress(event:ProgressEvent):void {}      
            private function handleComplete(event:Event):void {}      
            private function handleInitProgress(event:Event):void {}
            
            private function handleInitComplete(event:Event):void {
              var timer:Timer = new Timer(2000,1);
              timer.addEventListener(TimerEvent.TIMER, dispatchComplete);
              timer.start();
            }
            
            private function dispatchComplete(event:TimerEvent):void {
              dispatchEvent(new Event(Event.COMPLETE));
            }
            // IPreloaderDisplay interface methods.
            public function get backgroundColor():uint {
              return 0;
            }
            public function set backgroundColor(value:uint):void {}
            public function get backgroundAlpha():Number {
              return 0;
            }
            public function set backgroundAlpha(value:Number):void {}
            public function get backgroundImage():Object {
              return undefined;
            }
            public function set backgroundImage(value:Object):void {}
            public function get backgroundSize():String {
              return "";
            }
            public function set backgroundSize(value:String):void {}
            public function get stageWidth():Number {
              return 200;
            }
              public function set stageWidth(value:Number):void {}
            public function get stageHeight():Number {
              return 200;
            }
            public function set stageHeight(value:Number):void {}
        }
      }