A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: [RESOLVED] External Preloader FLA Not Working

  1. #1
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205

    resolved [RESOLVED] External Preloader FLA Not Working

    Hi all,

    I export all my stage assets to frame 1, so the external preloader FLA is the approach I've been using for a while to show the nice percentage before the real SWF is loaded. I've just finished with a project and I'm getting errors when the file is done loading.

    The file works fine as long as it's not it a Loader component (stand-alone SWF), but as soon as I try to get it in the preloader it gives me a ton of errors.

    The only major thing I did differently with this project is that a number of classes have their own individual Event.ENTER_FRAME listeners and that *I'm making a lot of stage calls* from various classes to get locations and what-not. I'm thinking it might have something to do with the latter, since I used the stage width/height for a lot of the positioning (stage.stageWidth/2, stage.stageHeight/2, etc).

    I'd still like to know why this is happening and what I should do in the future to avoid this again. Anyone have any ideas?

    Thanks,

    Jason
    Follow me on Twitter: http://twitter.com/jasondefra

  2. #2
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    Any ideas? Or perhaps I should elaborate a bit more? Let me know your thoughts, no matter how insignificant you think they might be. Thanks everyone.
    Follow me on Twitter: http://twitter.com/jasondefra

  3. #3
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    Here is a screenshot of my output:

    http://imgur.com/peQvz.jpg

    So the preloader.swf sees my main SWF (the numbers prior to the TypeError are loaded percentage), but it's saying that my Event.ENTER_FRAME listeners are being problematic. The thing is, when I run the main SWF without the preloader it works fine.

    I hope this makes my question a bit more clear.

    Here is the script that's in my LeftHand & RightHand classes:
    PHP Code:
    private function onLoop(e:Event):void
            
    {
                
    this.x=stage.mouseX;
                
    this.y=stage.mouseY;
            } 
    Last edited by jasondefra; 08-19-2009 at 01:46 PM.
    Follow me on Twitter: http://twitter.com/jasondefra

  4. #4
    lemon juice hurts your eyes florianvanthuyn's Avatar
    Join Date
    Jul 2005
    Location
    Merelbeke, Belgium
    Posts
    546
    It's probably because the swf hasn't been added to the stage yet and so it can't find it at first.
    Try using this listener:
    PHP Code:
    addEventListener(Event.ADDEDonAdd);

    private function 
    onAdd(e:Event):void
    {
       
    // stage can now be accessed

    in your external swf script so you know when the stage property is available.
    Florian Vanthuyne

    WAR AGAINST SOLVED THREADS
    mark yours as Resolved under Thread Tools!

  5. #5
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    THE SET UP:

    I have two SWFs (for the sake of explanation, they are called Preloader.swf and Main.swf). Main.swf has a document class that calls Main.as, and from there there is a number of additional classes. All library objects/assets are exported to frame 1 in Main.fla, so when Main.swf is executing all of the assets are loaded before any script is called, eliminating the opportunity to embed a preloader. Preloader.swf addresses this concern by calling in Main.swf using a Loader object. Preloader.swf keeps track of the bytesLoaded while you show your on-screen percentage and graphics (so the user isn't staring at a blank screen). When the Main.swf is completely loaded in the Loader, you get rid of all the preloading graphics/text and simply add the Loader to your stage. Voila, you SHOULD have a successful preloader loading your Main.swf.

    THE PROBLEM:

    Main.swf loads fine in Preloader.swf, but when it's completely loaded Flash gives you an error:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Main$iinit()

    THE SOLUTION

    I went into Main.swf and removed the Document Class. I created a new Symbol in Main.fla called it Main and exported it to Actionscript on Frame 1 (this should then use your Main.as provided it extends MovieClip). On Main.fla's stage, add a blank keyframe so you have a total of 2 keyframes. Drag Main out of your library onto the stage on Frame 2 at position x=0 y=0 with a "stop();" script. Publish your swf to make sure it still works (mine did).

    Run Preloader.swf and the TypeError goes away.

    THE EXPLANATION:

    I have NO clue why it works when I removed the Document Class AND put the Main MovieClip on Frame 2 (doesn't work if you put it on Frame 1). If you know why this is happening I'd love to hear it, this is way too hacky for my taste and I hate it, but at least it's working.

    ~Jason
    Follow me on Twitter: http://twitter.com/jasondefra

  6. #6
    lemon juice hurts your eyes florianvanthuyn's Avatar
    Join Date
    Jul 2005
    Location
    Merelbeke, Belgium
    Posts
    546
    My guess is that frames switch slower than code executes, so with your previous code, it tried accessing stage before it was added (the code started running the moment it got loaded 100%) and that gave you an error.
    With your setup right now, calling the stage only happens in the second frame, so it first has to go through frame 1 and that gives your preloader code the time to add the loader to the stage before its content tries reaching it.

    (Hope that makes some sense )
    Florian Vanthuyne

    WAR AGAINST SOLVED THREADS
    mark yours as Resolved under Thread Tools!

  7. #7
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    That actually makes perfect sense! I kinda assumed it was something along those lines, so it's good to hear someone else affirming my suspicions. In any case, Adobe KNOWS that 80+% (warning: made up statistic) of Flash apps NEED a preloader, you'd think they'd implement some sort of *something* that would just do it without having to use an additional preloader SWF.

    Maybe I'm simply unaware/ignorant to a class Adobe has implemented (it's been a while since I've skimmed through the LiveDocs library).

    How do you normally take care of preloading, Florian?
    Follow me on Twitter: http://twitter.com/jasondefra

  8. #8
    lemon juice hurts your eyes florianvanthuyn's Avatar
    Join Date
    Jul 2005
    Location
    Merelbeke, Belgium
    Posts
    546
    In Flash I tend to use a one frame preloader with EventListeners to follow the progress. I don't export my assets on the first frame (which will prevent a white screen from showing until the first frame is loaded), but put them all on a second frame which I skip after preloading.
    This way you can have it preload everything after the first frame (which should not be big at all, since it is a preloader) without having to bother with an external SWF which will obviously also show a white screen if it's a preloader with rich graphics.

    In Flex (AS developing) I've found a rather hackish way of accomplishing a preloader, since you don't have a timeline there.
    It consists of pushing the main class (Sprite or MovieClip) into a second frame of another class (has to be MovieClip so you have a timeline) which you then define as new "main" class.
    It works just fine, but you're right about that feature not being there in the AS3 language.. (or not that I know of)
    Florian Vanthuyne

    WAR AGAINST SOLVED THREADS
    mark yours as Resolved under Thread Tools!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center