Accessing the Document class in AS3

后端 未结 3 780
青春惊慌失措
青春惊慌失措 2020-12-31 06:28

How can instantiated classes access the Document class?

Even after I name the Document class using the Properties bar in Flash, att

3条回答
  •  有刺的猬
    2020-12-31 06:55

    You can use a singleton for your document class (Main, in your example), which allows you to access the instance from anywhere.

    public class Main extends Sprite {
        private static var _instance:Main;
        public static function get instance():Main { return _instance; }
    
        public function Main() {
            _instance = this;
           // etc...
        }
    
        // etc...
    }
    

    Then you access the Main instance like this:

    public class Other {
        public function Other() {
            Main.instance.usefulInstanceMethod();
        }
    }
    

    The document class is a pretty good candidate for the singleton pattern, because generally there should only be instance available.

提交回复
热议问题