actionscript 3 init()

后端 未结 4 1127
[愿得一人]
[愿得一人] 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: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.

提交回复
热议问题