Yamiko
Yamiko

Reputation: 5453

make a preloader class instead of having it in the document class

I have built a basic preloader that runs in my document class. I'm having trouble with it. I'm guessing its due to what a class can and can not access from the stage? theres 2 problems. the first is that I cant change the keyframe the stage is on from the class. the second is im getting an error 1009 if I comment that out.

package 
{


 import flash.display.MovieClip
    import flash.events.Event;
    import flash.events.ProgressEvent;

    public class Pre extends MovieClip
    {
        public function Pre()
        {
            loaderInfo.addEventListener(Event.COMPLETE,downloadFin);
            loaderInfo.addEventListener(ProgressEvent.PROGRESS,preloadProgress);
            function preloadProgress(progressEvent:ProgressEvent):void
            {
                var floatLoaded:Number=loaderInfo.bytesLoaded/loaderInfo.bytesTotal;
                var newW:Number=this.width*floatLoaded;
                this.Fill.width=newW;
            }
            function downloadFin(event:Event):void
            {
                trace('fin')
                //stage.gotoAndStop(3);//frame with game
            }
        }
    }
}

Upvotes: 0

Views: 286

Answers (1)

Mattias
Mattias

Reputation: 3907

I recommend you to dispatch an event when the preloader is ready, making yor preloader more generic. Then add a listener in the document class like this:

private function setupPreloader() : void
{
    preloader.addEventListener(Event.COMPLETE , onPreloaderComplete);
preloader.start();
}

private function onPreloaderComplete(event : Event) : void
{
preloader.removeEventListener(Event.COMPLETE, onPreloaderComplete);
preloader.dispose();

gotoAndStop(3);
}

Upvotes: 1

Related Questions