Flash CS5 reference a display object in the constructor of a non-document class

后端 未结 3 1435
感动是毒
感动是毒 2021-01-25 02:17

After learning of this excellent method of accessing an object placed on the stage in Flash CS5 in a class other than the document class (found in this thread), I have run into

3条回答
  •  深忆病人
    2021-01-25 02:57

    The fact that you can not access the instances on the stage in a class' constructor but can in another function of the class (that you call later I assume) makes me think that stage is not available to your constructor when it is being called.

    Check to make sure that your Game.as document class is set up like this, although it probably is since you followed my method:

    public function Game() {
        addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
        //DO NOT CREATE CLASS INSTANCES IN HERE
    }
    
    private function init(e:Event):void{
        //In here is where we create instances of classes
    }
    

    Additionally, set your stage data type to Stage instead of Object in your Player class's constuctor's paramater for good coding practice.

提交回复
热议问题