AS3 从另一个 class 访问 Main class 中的一个方法。报错?

AS3 Access a method inside the Main class from another class . Gives error?

我在使用 actionScript 时遇到了问题,我试图使用简单的一行代码来访问文档 Class(主要)中的方法,但每次我都遇到错误。我在舞台上用电影剪辑尝试了相同的代码,效果很好。

主要Class链接到fla :

package {

import flash.display.*;
import flash.events.*;


public class Main extends MovieClip {


    public function Main() {

        if (stage) {
            init();

        }
        else addEventListener(Event.ADDED_TO_STAGE, init);


    }
    private function init(e:Event = null):void {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        button.addEventListener(MouseEvent.CLICK,_click);

    }
    private function _click(e:MouseEvent):void {
        var l:Leecher = new Leecher();
        l.leech();
    }


    public function callMe():void {
        trace("hey nice work");
    }

}

}

里彻 Class :

package {

    import flash.display.*;

    public class Leecher extends MovieClip {

        public function leech():void
        {
            trace(" leech function ");

            Main(parent).callMe();       // output null object
            Main(root).callMe();        // output null object
            Main(Main).callMe();       // output null object


        }

    }

}

相同的代码,但 class 链接到舞台上的按钮

package 
{

    import flash.display.*;
    import flash.events.*;


    public class Button extends MovieClip {


        public function Button() {
            this.addEventListener(MouseEvent.CLICK,r_click);
        }
        private function r_click(e:MouseEvent):void {
            var l:Leecher = new Leecher();
            l.leech();
            Main(parent).callMe();  // hey nice work
            Main(root).callMe();    // hey nice work
            Main(Main).callMe();    // output null object

        }
    }

}

错误是因为当该代码运行时,Leecher 实例尚未添加到显示列表中,因此没有 parentrootstage(所以 parentnull)。

这里是正在发生的事情的细目分类(用代码注释解释):

private function _click(e:MouseEvent):void {
    //you instantiate a new Leecher object
    var l:Leecher = new Leecher();

    //you call leech, but this new object does not have a parent because you haven't added it to the display list (via `addChild(l)`)
    l.leech();
}

//your saying parent should be of type Main, then call the callMe method.  However, parent is null because this object is not on the display list
Main(parent).callMe(); 

//same as above, except using root
Main(root).callMe();

//Here you are saying the Main class is of type Main (which since Main is a class and not an instance of Main will error or be null)
Main(Main).callMe();

显示 object 的 rootparentstage 变量仅在所述显示 object 添加到显示列表时才会填充.在 rootstage 的情况下,还必须添加 parent(以及任何 grand parents),以便最上面的 parent/grandparent 是 stage.

因此,您需要等到通过侦听 Event.ADDED_TO_STAGE 事件可以安全地访问 parent

private function _click(e:MouseEvent):void {
    var l:Leecher = new Leecher();

    //call the leech method once the child has been added to the stage and has a parent value
    l.addEventListener(Event.ADDED_TO_STAGE, l.leech, false, 0, true);
    addChild(l);
}

如果执行上述操作,则需要向 leech 方法添加一个可选的事件参数,否则会出现错误:

public function leech(e:Event = null):void
{

为了使您的 Main class 易于访问,您可以使用静态引用。 静态变量不绑定到 object 的实例,而是绑定到 class 本身。

public class Main extends MovieClip {
    //create a static var that holds a reference to the root/main instance 
    public static var main:Main;

    public function Main() {
        //assign the static var to this (the instance of Main)
        main = this;

        //...rest of code

如果这样做,您可以通过执行 Main.main 来评估代码中任何位置的根,因此在您的示例中您可以执行以下操作:

Main.main.callMe();

我建议在疯狂使用它们之前多阅读有关静态变量的内容。执行我刚刚展示的内容以便轻松参考您的文档 class / root 是安全的,但在其他情况下,最好注意一些内存和性能上的细微差别。