Skip navigation
Anouka31
Currently Being Moderated

Preloader plays swf before load complete

May 30, 2012 6:56 PM

Tags: #as3 #preloader #external_swf #preloader_problem

My Flash (CS4/AS3) website has turned out to be pretty huge- it's my first site and I'm ok with it taking a while to load at this time.. planning to clean things up later but need it running in the meantime. I needed a preloader at the beginning, but as so many actions in the site were tied to frame numbers, I decided to make the index.html a swf containing only the preloader and a UI loader, which loads the main swf. This works ok, except the site appears to load before the preloader is finished, so the loading bar overlaps the site and the site is only half functional during these few seconds. Anyone have any advice to keep the site from loading until the preloader finishes? The website is www.bespokeglasstile.com

and the code I'm using for the preloader is below.

 

var imageURL:String = "bespoke_glass_tile.swf";
var request:URLRequest = new URLRequest(imageURL);
bgtUI.load(request);

 

progressBar.source = bgtUI;
progressBar.addEventListener(ProgressEvent.PROGRES S, progressHandler);
progressBar.addEventListener(Event.COMPLETE, completeHandler);

 

function progressHandler (event:ProgressEvent):void {
status_txt.text = int(event.currentTarget.percentComplete) + "%";
}

 

function completeHandler (event:Event): void {
progressBar.removeEventListener(ProgressEvent.PROG RESS, progressHandler);
progressBar.removeEventListener(Event.COMPLETE, completeHandler);
removeChild(progressBar);
status_txt.text = "";
}

 

After searching several forums, I came across others with the same issue and I tried to extrapolate from advice given to them by trying the following:

adding addChild(bgtUI); to the completeHandler function

making the bgtUI.alpha  = 0; and then changing it to 100 in the completeHandler function

and there is a stop(); on the first frame of the loaded swf.

 

Thanks in advance!

 
Replies
  • Currently Being Moderated
    May 31, 2012 5:13 AM   in reply to Anouka31

    Using the addChild(bgtUI) in the completeHandler function should be sufficient.  If it is not working for you, show all of the code relative to it.

     
    |
    Mark as:
  • Currently Being Moderated
    May 31, 2012 2:46 PM   in reply to Anouka31

    I don't see how you get bgtUI into the picture.  What do you do to create it?

     
    |
    Mark as:
  • Currently Being Moderated
    May 31, 2012 4:57 PM   in reply to Anouka31

    If the UILoader is added manually to the stage, it is already in the 'addChild' state - you would have had to remove it first before adding it.   You are probably better off making it invisible/visible instead... bgtUI.visible = false; at the start, and bgtUI.visible = true; after loaded.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 1, 2012 6:31 AM   in reply to Anouka31

    If you are testing in a browser, I recommend clearing your cache each time you test.  You should put a trace in the in the functions to see what the percentComplete property looks like during and when the completeHandler function gets triggered.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 8, 2012 4:28 AM   in reply to Anouka31

    What you say you see sounds rright but you are not tracing what I suggested you should.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 8, 2012 3:51 PM   in reply to Ned Murphy

    The way that loading .swf files display is different and unique to each browser/platform. What you are seeing is result of the loading process. When some visual content in the loading .swf is available, the Flash plugin will display it. Some browser/platform combinations won't display the .swf until it's fully loaded. You have no control over this arbitrary process. If you don't want the .swf to be available until it is completely loaded, then you need to "hide" it in some way from the user.

     

    Using the visible property may not work as expected because the Loader object may not react to that property's values as you expect. In other words, try something else. The alpha property may be more useful.

     

    I usually use the position of the loading object. I set the y property of the loader to -1000, or something like that. This pushes the loader up above the actual stage area. When the loading is finished you can then move the object down to the stage.

     

    Another useful method is to draw a stage sized sprite and fill it with a color. Add the sprite to the stage. Then you can add the loader to the stage behind this sprite. When it is loaded, or when you want the user to be able to use it, just change its position in the display list.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 11, 2012 11:17 AM   in reply to Anouka31

    Try this simplified version of your code:

     

     

     

    import flash.display.Loader;

    import flash.events.*;

    import flash.net.URLRequest;

     

     

    var bgtUI:Loader = new Loader();

    var imageURL:String = "moveImage.swf";

    var urlReq:URLRequest = new URLRequest(imageURL);

    bgtUI.load(urlReq);

    configureListeners(bgtUI.contentLoaderInfo);

    addChild(bgtUI);

     

     

    progressBar.source = bgtUI;

    progressBar.addEventListener(ProgressEvent.PROGRESS, progressHandler);

    progressBar.addEventListener(Event.COMPLETE, completeHandler);

     

     

    function progressHandler(event:ProgressEvent):void

    {

              status_text.text = "bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal;

    }

     

     

    function completeHandler(event:Event):void

    {

              progressBar.removeEventListener(ProgressEvent.PROGRESS, progressHandler);

              removeChild(progressBar);

              removeChild(status_text);

    }

     

     

    function configureListeners(dispatcher:IEventDispatcher):void

    {

              dispatcher.addEventListener(Event.COMPLETE, completeHandler);

              dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);

    }

     

    There were a number of things that I needed to change in order to get the loader working:

    1. Don't name a URLRequest or any other object with a reserved word. "Request" is such a reserved word. It may or may not throw an error at runtime, but it will most likely cause havoc.

    2. You can't attach a progress event directly to a component. You need to use an event dispatcher.

    3. While blanking the text property of the textfield on the stage will make the text go away, you really want the text field to go away. You can just use removeChild() to do that.

    4. Likewise with the progress bar. Just use a simple removeChild().

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 12, 2012 5:06 PM   in reply to Anouka31

    Those errors all appear to involve properties of a UILoader.  For what Rob has offered there is no longer a UILoader component, but instead a coded Loader object.  I don't see that code in anything that is shown, so do you have code somewhere else for the UILoader you had originally?

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points