AS3从另一个类访问Main类中的方法。给出错误?

时间:2017-06-07 19:21:01

标签: actionscript-3

我在使用动作脚本时遇到问题,我试图使用简单的一行代码来访问Document Class(Main)中的方法,但每次我都遇到错误。我在舞台上用movieClip尝试了相同的代码,效果很好。

主要类别与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");
    }

}

}

Leecher课程:

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


        }

    }

}

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

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

        }
    }

}

1 个答案:

答案 0 :(得分:1)

错误是因为当代码运行时,Leecher实例尚未添加到显示列表中,因此没有parentroot或{{ 1}}(所以stageparent)。

以下是对所发生情况的细分(解释为代码注释):

null

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(); root&仅当将所述显示对象添加到显示列表时,才填充显示对象的parent个变量。在stage&的情况下root父母(以及任何祖父母)也必须添加,以便最顶层的父/祖父母是stage

因此,您需要等到stage事件可以安全访问parent

Event.ADDED_TO_STAGE

如果您执行上述操作,则需要向leech方法添加可选的事件参数,否则您将收到错误:

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);
}

要使Main类易于访问,可以使用静态引用。 静态变量不是绑定到对象的实例,而是绑定到类本身。

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

如果你这样做,你可以通过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

我建议在疯狂使用之前阅读更多静态变量。做我刚刚展示的文档类/ root的简单参考是安全的,但在其他情况下,有一些内存&表现细微差别,最好注意。