views:

471

answers:

1

I'm trying to find the correct event to listen for that will ensure that my component parameters are available for use so I can initialize my component.

Most examples I have seen online use Event.INIT attached to loaderInfo.

loaderInfo.addEventListener(Event.INIT, initHandler);

From my experience, that event only fires on the first frame of the movie.

Other people use Event.COMPLETE, which fires after Event.INIT, to ensure that the component and parameters are available for use. Once again, the event seems to only fire on the first frame of the movie. This makes sense since it is attached to the loaderInfo property of the component.

Below is the class for a very simple component which shows exactly what I'm talking about. Attach this class to a movieclip in the Properties dialog and the Component Definition dialog (I'm not going to tell you how to make a component since you probably know), then drag the resulting component to the stage and set the "Test var" parameter to "TEST_VAR_CHANGED".

When you render the movie with the component in the first frame you'll see:

constructor null
initHandler TEST_VAR_CHANGED
completeHandler TEST_VAR_CHANGED

When you render the movie with the component in the second frame you'll only see:

constructor null

So...which event do I listen to to guarantee component parameters are available before I run my init handler?

Component class:

package
{
    import flash.display.MovieClip;
    import flash.events.Event;

    public class ComponentEventTest extends MovieClip
    {
        [Inspectable(name="Test var", type="String")]
        public var testVar:String;    

        function ComponentEventTest()
        {
            trace('constructor', testVar);
            loaderInfo.addEventListener(Event.INIT, initHandler);
            loaderInfo.addEventListener(Event.COMPLETE, completeHandler);
        }

        private function initHandler(evt:Event):void
        {
            loaderInfo.removeEventListener(Event.INIT, initHandler);
            trace('initHandler', testVar);
        }

        private function completeHandler(evt:Event):void
        {
            loaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
            trace('completeHandler', testVar);
        }        
    }
}
A: 

Edit: Ok you can wait for the first frame to be rendered:

Listening to the exit frame if available (flash player 10) or enter frame:

    function ComponentEventTest()
    {
        trace('constructor', testVar);
        addEventListener(Event.ENTER_FRAME, initHandler);
    }

    private function initHandler(evt:Event):void
    {
        removeEventListener(evt.type, initHandler);
        trace('initHandler', testVar);
    }
Patrick
Thanks for your reply.Actually, the ADDED_TO_STAGE event occurs before the INIT event and displays testVar as null also.
shawnc
Sorry i have forgotten it was a component, i have edit the answer.
Patrick
Thanks for the answer, but the linked page is AS2 (AS3 didn't exist in 2004) and doesn't solve the problem of when to know that the component parameters are loaded and component initialization can proceed. That example just assumes that when the constructor is called you can proceed with initting.
shawnc
Ok change for as3 ;)
Patrick
:-) Yeah, I know, I did, but AS2 components work differently than AS3. With AS2 you could handle the onLoad event and everything would work great. AS3 doesn't seem to have an explicit "I am initialized" event unless you listen to the component loader and then it *only* works on frame 1. Thanks for the try, though! :-)
shawnc
Yes, you're absolutely correct. I added listeners for all the events that a movieclip can listen for and the EXIT_FRAME event was the first event that fires in both frame 1 and other-than-frame-one (frame 2+) where the component parameters are fully loaded. Interestingly, everyone seems to listen for INIT or COMPLETE on the loaderInfo object. EXIT_FRAME actually fires *before* INIT and COMPLETE *and* has the component parameters fully loaded. Pretty sweet. Thanks!
shawnc
In your comment you wrote, "EXIT_FRAME if available (flash player 10)". I checked the live docs and both EXIT_FRAME and ENTER_FRAME are listed for flash player 9. Is there something you know that I should watch out for? http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/events/Event.html#EXIT_FRAME
shawnc
Weirdly, FlashDevelop doesn't even list EXIT_FRAME in its command completion.
shawnc