actionscript 3 init()

后端 未结 4 1126
[愿得一人]
[愿得一人] 2020-12-16 21:16

I have often seen an init() within the constructor of AS3 classes, sometimes even being the only code in the constructor. Why would it be useful to do this, if you could si

相关标签:
4条回答
  • 2020-12-16 21:45

    In ActionScript 3, constructor code is always interpreted rather than compiled. I believe moving the code into an init() function may allow it to be compiled and optimized.

    http://blog.pixelbreaker.com/flash/as30-jit-vs-interpreted/

    0 讨论(0)
  • 2020-12-16 21:45

    Another reason can be that you need a reference to the stage or a parent container and is too lazy to set up a ADDED_TO_STAGE listener. Then you would have instantiate the class first, add it to the container and then call init() once it's on the displaylist.

    0 讨论(0)
  • 2020-12-16 21:57

    The reason I have done it is so that I can re-initialize a class without creating a new instance of it. The init() method works as basically a "reset" button then, if you code it right, allowing you to return the class to its initial state while, for instance, allowing any variables that have been set to remain set.

    Depending on how you code it, of course.

    0 讨论(0)
  • 2020-12-16 21:58

    Programmers new to AS3 often have problems referencing the stage (the well known 'it's not there' situation).

    By doing ... :

    public function ClassName()
    {
        super();
        addEventListener( Event.ADDED_TO_STAGE, init, false, 0, true );
    }
    
    private function init( event : Event ) : void
    {
        removeEventListener( Event.ADDED_TO_STAGE, init );
        // Reference stage.stageWidth;
        // Call init after some sort of load completion initialized in the constructor
    }
    

    ... it's easily fixed.

    Or sometimes you initialize an XML loader in the constructor, and then call the initialize function upon load completion.

    0 讨论(0)
提交回复
热议问题