as3 calling a function in Main.as Document Class from another class

前端 未结 3 1579
天涯浪人
天涯浪人 2021-01-27 01:17

I am sure this is a popular question but I can\'t find the exact answer I need. I simply need to access a function or functions created in the Main.as document class. I have tri

3条回答
  •  遇见更好的自我
    2021-01-27 01:25

    The cleaner way is to simply pass a reference to Main to the constructor of the class you want to be able to access it.

    For example, your AnotherClass could look like this:

    class AnotherClass
    {
        private var _main:Main;
    
        public function AnotherClass(main:Main)
        {
            _main = main;
            _main.test(); // Success!
        }
    }
    

    And your main class:

    class Main
    {
        public function Main()
        {
            var another:AnotherClass = new AnotherClass(this);
        }
    
        public function test():void
        {
            trace("Success!");
        }
    }
    

提交回复
热议问题