Goupil
Goupil

Reputation: 11

As3 project with Preloader

I want to use the Preloader provided by FlashDevelop but it does not react as it should.

My loader tells me 100% when no file has been downloaded.

The code should display a trace() containing the percent intermediary but does not

Could you help me?

Mains.as

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

   [Frame(factoryClass="Preloader")]
   public class Main extends Sprite
   {
       public function Main():void
       {
           if (stage) init();
           else addEventListener(Event.ADDED_TO_STAGE, init);
       }

       private function init(e:Event = null):void
       {
           removeEventListener(Event.ADDED_TO_STAGE, init);
           // entry point
           var imgRequest:URLRequest         = new URLRequest("http://next-web.be/actionscript/0.jpg");
           var img:Loader = new Loader();
               img.load(imgRequest);
           addChild(img);
       }
   }
}

Preloader.as

package
{
   import flash.display.DisplayObject;
   import flash.display.MovieClip;
   import flash.display.StageAlign;
   import flash.display.StageScaleMode;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.ProgressEvent;
   import flash.utils.getDefinitionByName;

   import flash.display.Sprite;

   public class Preloader extends MovieClip
   {
       private var bar:Sprite;

       public function Preloader()
       {
           if (stage) {
               stage.scaleMode = StageScaleMode.NO_SCALE;
               stage.align = StageAlign.TOP_LEFT;
           }
           addEventListener(Event.ENTER_FRAME, checkFrame);
           loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
           loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);

           // TODO show loader
           bar = new Sprite();
           bar.graphics.lineStyle(1, 0x4444ff, 1, true);
           bar.graphics.drawRect(0, 0, 100, 6);
           bar.x = stage.stageWidth / 2 - bar.width / 2;
           bar.y = stage.stageHeight / 2 - bar.height / 2;
           addChild(bar);
       }

       private function ioError(e:IOErrorEvent):void
       {
           trace(e.text);
       }

       private function progress(e:ProgressEvent):void
       {
           // TODO update loader
           bar.graphics.lineStyle(0, 0, 0);
           bar.graphics.beginFill(0x8888ff);
           bar.graphics.drawRect(1, 1, (e.bytesLoaded / e.bytesTotal) * 98 , 4);
           bar.graphics.endFill();
           trace( "loading:" + (e.bytesLoaded / e.bytesTotal) * 100 );
       }

       private function checkFrame(e:Event):void
       {
           if (currentFrame == totalFrames)
           {
               stop();
               loadingFinished();
           }
       }

       private function loadingFinished():void
       {
           removeEventListener(Event.ENTER_FRAME, checkFrame);
           loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
           loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);

           // TODO hide loader
           removeChild(bar);
           bar = null;

           startup();
           //stop();
       }

       private function startup():void
       {
           var mainClass:Class = getDefinitionByName("Main") as Class;
           addChild(new mainClass() as DisplayObject);
       }
   }
}

Upvotes: 0

Views: 2264

Answers (2)

Goupil
Goupil

Reputation: 11

After a continued search, I realized my mistake.

Until now, I always use "loader" but this function does not include my pictures to swf file. So I use a library swc allowing me to generate a complete swf.

Upvotes: 0

Staven
Staven

Reputation: 3165

You need to register your listener on the contentLoaderInfo property of the Loader you use to download the data (in your case img.contentLoaderInfo).

In your code, you register progress on loaderInfo, which is a field of your Preloader class (inherited from MovieCLip), and will give you the progress on loading the SWF that contains the Preloader class.

public class Preloader extends MovieClip
{
    public function Preloader()
    {
        // This is wrong.
        loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
        // Function `progress` will show the progress of loading your SWF file,
        // *not* the JPEG you're loading in class Main.
    }
}

You need to register your listener on the contentLoaderInfo property of the Loader you use to download the data (in your case img.contentLoaderInfo).

img.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);

Of course, then it will show only the progress of loading the JPEG file. You'll also need to either pass the Loader object (or just the contentLoaderLinfo) to the Preloader somehow, or include the event handler in your Main class.

Upvotes: 2

Related Questions