views:

1346

answers:

5

I have a preloader in my flex application:

public class Preloader extends DownloadProgressBar 
{

    private var _preloader:PreloaderAnimation;

    public function Preloader()
    {
        super();

        _preloader = new PreloaderAnimation;
        addChild(_preloader);
    }

    public override function set preloader(preloader:Sprite):void 
    {                   
        preloader.addEventListener(ProgressEvent.PROGRESS  , onSWFDownloadProgress );
        preloader.addEventListener(Event.COMPLETE          , onSWFDownloadComplete );
        preloader.addEventListener(FlexEvent.INIT_PROGRESS , onFlexInitProgress    );
        preloader.addEventListener(FlexEvent.INIT_COMPLETE , onFlexInitComplete    );
    }

    .........

    private function onFlexInitComplete( event:FlexEvent ):void 
    {
        dispatchEvent(new Event(Event.COMPLETE));
    }
}

When the Flex Initialize is complete the preloader dispatches an Event.COMPLETE. But I want to be able to listen to the event in my flex app and do stuff when it dispatches. When it dispatches the preloader removes itself that's why its crucial. Any idea on how I could listen from my flex app?

Regards Adlertz

A: 

I am not certain exactly what it is you are trying to achieve. If you are just wanting to do something at the time the application is initialized or creation is complete you can use the creationComplete or intialize events on the application. Is there a reason you need to know as soon as the preloader is complete (assuming that would be different than the creationComplete event from the application, I don't think it would be) ?

Update:

so you are just wanting to be able to listen to the Complete event from the preloader correct?

Try this: create an initalize event handler on the application and try to attach an event listener to the preloader. it would look something like this (this is not tested so it may have typos, etc)

public function applicationInitalize_handler ( e:FlexEvent ) : void
{
Application.preloader.addEventListener(Event.COMPLETE,myEventHandler);
}

see if that could work.

Ryan Guill
I tried Application.application.preloader.addEventListener(Event.COMPLETE,onPreloaderComplete) in my initialize function in my application, but I got a Error #1009: Cannot access a property or method of a null object reference.
how are you setting up your preloader? are you putting it as a property on the mx:application tag?
Ryan Guill
yes I am. Is that recognized after I add my listener?
A: 

The reason why I need to know when the preloader is complete is because I want to play a movie directly after, where the first frame in the movie looks like the image of the preloader.

The preloader is displayed until you dispatch the Event.COMPLETE event from your preloader.

If you want to display something between your preloader completing and the application being displayed then you would need to do it inside your preloader prior to dispatching Event.COMPLETE. If you do not dispatch the COMPLETE event your preloader will be displayed forever.

(If you're curious, look at the Flex Preloader source code in the 'displayClassCompleteHandler' function)

Sly_cardinal
A: 

The Preloader's INIT_COMPLETE event is fired after the preloader receives the Application's CREATION_COMPLETE event.

The order of initialisation is this:

  • Preloader starts loading your app;
  • Application downloaded, starts initialising;
  • Application is initialised, dispatches CREATION_COMPLETE;
  • Preloader receives Application.CREATION_COMPLETE, dispatches INIT_COMPLETE;
  • Your preloader class receives the Preloader's INIT_COMPLETE;
  • Your preloader dispatches the COMPLETE event;
  • The Preloader removes your preloader class and dispatches the (private) PRELOADER_DONE event;
  • Your application is displayed.

What this means in this case is that the Application.CREATION_COMPLETE is equivalent to the Preloader.INIT_COMPLETE - if you want to know when the preloader dispatches INIT_COMPLETE then listen for CREATION_COMPLETE.

You can confirm all of this by looking through the source code for the Preloader class.

Sly_cardinal
A: 

Flex.INIT_COMPLETE is triggered when flash application has completed initialization. so its dispatched exactly after the initialize event of main applcation.

A: 

The application's APPLICATION_COMPLETE event seems to be issued just after the Preloader fires the Event.COMPLETE event... just listen to this?

bob