A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Help with Preloader class in external .as file

  1. #1
    cake! Skribble_Style's Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    550

    Help with Preloader class in external .as file

    Hey guys. I'm trying to wrap my head around AS3 and it isn't going well. I'm getting stuck on the most basic of problems it's making me pretty angry. I'm trying to do something as simple as reference the stage and I just can't figure it out; something that usually takes me seconds has eaten up 2 days of my time.

    I have a Main.fla that outputs Main.swf; it contains no script, only library assets. I have a document class set up using Main.as, which itself has 1 trace and code to run the Preloader class, which is held in Preloader.as.

    The following is Main.as:

    Actionscript Code:
    package
    {
        import flash.display.MovieClip;
       
        public class Main extends MovieClip
        {
            //START
            //     
            public function Main()
            {
                trace(" Main class has been instantiated correctly. Carry on, human. ");
               
                //Load the preloader
                var gameLoader:Preloader = new Preloader();
            }
            //
            //END
        }
    }


    The following is the Preloader class:

    Actionscript Code:
    package {
        import flash.display.MovieClip;
        import flash.events.Event;
       
        public class Preloader extends MovieClip {
            //START
            //
            public function Preloader() {
                trace(" Preloader class has been instantiated. Carry on, human. ");
               
                addEventListener(Event.ENTER_FRAME, preload);
               
                function preload(event:Event):void {
                    var bytestotal = stage.loaderInfo.bytesTotal;
                    var bytesloaded = stage.loaderInfo.bytesLoaded;
                    var pctLoaded:int = bytesloaded * 100 / bytestotal;
                    trace(pctLoaded)
                    if (bytesloaded >= bytestotal) {
                        removeEventListener(Event.ENTER_FRAME, preload);
                    }
                }
            }
            //
            //END
        }
    }

    Only Flash doesn't seem to like this. I am apparently referencing a null object or property on line 14 of Preloader.as. After doing much reading I found out that you cannot access the stage like in AS2, and you need to instance DisplayObject so it is loaded in to some magical "Display List" which I can't find anything on in the Documentation.

    Or something to that effect. I'm really confused by the whole thing. Someone else told me I shouldn't be referencing stage in my classes, but if thats true then how am I meant to keep everything in classes?

    I am lost in a sea of programming architecture I know nothing about, and I can't seem to get any straight answers from anybody I ask. So I come to you, noble Flashkitters. I would be eternally grateful for any help.


    Thanks.

    Brendan
    Last edited by Skribble_Style; 03-27-2010 at 12:32 AM.
    Skribble

    Its not that im lazy, I just dont care.

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    First of all functions within functions is no good and should be avoided. Second there is no reference to the stage. One possibility is to create a second function in Preloader
    PHP Code:
    public function preloadNow(mStage:DisplayObjectContainer):void
    {
    myStage mStage;
    addEventListener(Event.ENTER_FRAMEpreload);
    }
    private function 
    preload(event:Event):void {
                    var 
    bytestotal myStage.loaderInfo.bytesTotal;
                    var 
    bytesloaded myStage.loaderInfo.bytesLoaded;
                    var 
    pctLoaded:int bytesloaded 100 bytestotal;
                    
    trace(pctLoaded)
                    if (
    bytesloaded >= bytestotal) {
                        
    removeEventListener(Event.ENTER_FRAMEpreload);
                    }
                } 
    Don't forget to add
    private var myStageisplayObjectContainer;

    and in the Doc class:
    PHP Code:
    var gameLoader:Preloader = new Preloader();
    gameLoader.preloadNow(stage); 
    Last edited by cancerinform; 03-27-2010 at 11:31 AM.
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    cake! Skribble_Style's Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    550
    Ahh thank you, it works!

    So DisplayObjectContainer is literally the container that is displaying the movie, and you're referencing it through myStage.

    What I don't understand is why another mStage variable is needed? Could you possibly explain that for me? If not, thanks for the help, it is very much appreciated! I learned more from your 1 post than I did searching 2 days for a solution.

    Thanks again.

    Brendan
    Skribble

    Its not that im lazy, I just dont care.

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    DisplayObjectContainer is a superclass for the stage and also has the property loaderInfo. You need to create a new variable myStage, which is not local and has as its value the function parameter. Otherwise you cannot use it in the preload function.
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    cake! Skribble_Style's Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    550
    Ok I'm pretty positive I get it now. Passing stage from Main.as to Preloader.as through the function and then grabbing variable and putting it in to a private variable to use in the class. Thanks!

    One last question before I venture off:
    How do I use an external class to grab a movieclip from the library, and then add it to the stage using addChild? I'm assuming I have to reference the Movieclip I want to use, but I am not sure how.

    This is another thing I searched along side my previous problem and found limited results. Any help on this would be very welcome thanks!

    Brendan
    Skribble

    Its not that im lazy, I just dont care.

  6. #6
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You need to export the movieclip for actionscript. The identifier (for example Clip) is automatically the classname. You don't need to write a special class. Then you just call

    var myClip:Clip = new Clip();
    addChild(myClip);
    - The right of the People to create Flash movies shall not be infringed. -

  7. #7
    cake! Skribble_Style's Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    550
    Hmmm, thanks its all loading properly, but it still doesn't seem to be working. I ran a trace for "myClip" and it comes up with [object PreloaderGUI].

    PreloaderGUI being the Movieclip containing my Preloader. However it doesn't seem to be getting added to the stage.

    Actionscript Code:
    var myClip:PreloaderGUI = new PreloaderGUI();
    addChild(myClip);
    trace(myClip);
    Last edited by Skribble_Style; 03-28-2010 at 09:52 AM.
    Skribble

    Its not that im lazy, I just dont care.

  8. #8
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You need to explain what you want to do and for what you need the movieclip etc. I just answered your question how to add a movieclip from the library.
    - The right of the People to create Flash movies shall not be infringed. -

  9. #9
    cake! Skribble_Style's Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    550
    I appreciate you taking the time to help, but the code you supplied isn't adding anything to the stage, and being the complete newbie I am at AS3 I'm kinda flying blind here.

    PreloaderGUI is just a movieclip with some graphics in it, along with some text to display the percentage loaded.
    Skribble

    Its not that im lazy, I just dont care.

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