How to initialize Flash ActionScript 3 (AS3) components?

I am trying to find the right event for listening that will make my component settings available so that I can initialize my component.

In most examples, I saw an online use of Event.INIT attached to loaderInfo.

loaderInfo.addEventListener(Event.INIT, initHandler);

In my experience, this event is triggered only in the first frame of the film.

Other people use Event.COMPLETE, which runs after Event.INIT, to make sure that the component and parameters are available for use. Again, the event seems to fire only in the first frame of the film. This makes sense because it is bound to the loaderInfo property of the component.

Below is a class for a very simple component that shows exactly what I'm talking about. Attach this class to the movie clip in the Properties dialog box and in the Component Definition dialog box (I will not tell you how to make a component, as you probably know), then drag the resulting component onto the scene and set "Test var" , parameter "TEST_VAR_CHANGED".

When rendering a movie with a component in the first frame, you will see:

constructor null
initHandler TEST_VAR_CHANGED
completeHandler TEST_VAR_CHANGED

When you shoot a movie with a component in the second frame, you will only see:

constructor null

So ... what event do I listen to ensure that component parameters are available before my initialization handler starts?

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);
        }        
    }
}
+3
source share
1 answer

Edit: , , :

exit frame, (flash player 10) 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);
    }
+2

Source: https://habr.com/ru/post/1736193/


All Articles