views:

716

answers:

3

Hi,

I'm trying to set up a preloader to show the load progress of the main swf, which has all of it's assets embedded. I found the factory class method as described on bit-101, but no load progress ever seems to show. The swf loads fine, but the bytesLoaded is always equal to bytesTotal and therefore the preloader is never called.

This is the factory class I'm using

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

 /**
  * ...
  * @author DefaultUser (Tools -> Custom Arguments...)
  */
 public class Factory extends MovieClip
 {
  private var preloader:Preloader;

  public function Factory()
  {
   stop();
   stage.scaleMode = StageScaleMode.NO_SCALE;
   stage.align = StageAlign.TOP_LEFT;
   addEventListener(Event.ENTER_FRAME, enterFrame);
  }

  public function enterFrame(e:Event):void
  {
   trace(root.loaderInfo.bytesLoaded, root.loaderInfo.bytesTotal);

   if (framesLoaded == totalFrames) {
    removeEventListener(Event.ENTER_FRAME, enterFrame);
    nextFrame();
    init();
   }
   else {
    var percent:Number = root.loaderInfo.bytesLoaded / root.loaderInfo.bytesTotal;
    trace(percent);
   }
  }

  private function init():void
  {
   var mainClass:Class = Class(getDefinitionByName("Main"));
   if (mainClass) {
    var mainApp:Object = new mainClass();
    addChild(mainApp as DisplayObject);
    mainApp.init();
   }
  }

 }

}

The trace called on the enterFrame event shows that bytesLoaded and bytesTotal are equal.

And this is the Main class called when the swf loads.

package 
{
 import flash.display.Sprite;

 [Frame(factoryClass="src.Factory")]

 public class Main extends Sprite
 {
  [Embed(source="images/FinalImg01.jpg")]
  private var Image1:Class;
  [Embed(source="images/FinalImg02.jpg")]
  private var Image2:Class;
  [Embed(source="images/FinalImg03.jpg")]
  private var Image3:Class;

  public function init():void
  {
   trace("loaded");
  }
 }

}

This correctly calls the Factory class first, and then instansiates Main and calls init(). But as I mentioned above the preloading is never shown as it seems to be loading everything straight away.

Is this a problem with the way I'm embedding the images or because I'm testing locally?

Anybody else come across these issues?

Thanks Rich

A: 

I've successfully used this same method in all of my games, it has to be tested remotely, as local testing pretty much auto loads all bytes before the loader can react.

So yes, it is because you are testing locally. Upload the SWF to your webserver and you can see the actual load progress from there. Artificially bloating your file for the purposes of testing won't hurt as well, if you have a fast connection.

JStriedl
A: 

What version of Flash are you using? I know Pre-loading works very differently in CS2 then it does in CS3. I have had a similar problem getting a pre-loader to work in CS3 a while back.

http://www.senocular.com/flash/tutorials/faq/

To summarize with AS3 the entire library of a SWF file will be downloaded before any other action is taken. To fix this you can tell each library item not to load in frame 1, and then setup your preloader in the first frame.

Another solution would be to have a separate SWF file for doing the pre-loading. As soon as your pre-loader SWF loads up it could use the Loader object to load your larger swf file. The Loader object then provides ways to obtain how much data has been loaded through events.

A third solution would be to upload your SWF file to a site like MochiAds and allow them to provide the pre-loading capabilities. If you are making a game this would be the easiest solution.

Joshua Jewell
A: 

I'm not using Flash, so the not expoting on first frame solution mentioned above isn't an option because I don't have direct access to the library or timeline. The factory method is a way to achieve this in pure actionscript, neccessary if you are developing outside of the Flash IDE.

After some more research, I found the problem came from the implementation with FlashDevelop. There's actually a built in template in the latest FlashDevelop release that achieves the same result, without unneccesarily importing a lot of Flex components.

Check out this post if you need any more information

http://www.flashdevelop.org/community/viewtopic.php?f=13&t=3365

rlayte