开发者

How to initialize Flash ActionScript 3 (AS3) components?

开发者 https://www.devze.com 2022-12-22 05:07 出处:网络
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.

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);
        }        
    }
}


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);
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消