【问题标题】:AS 3.0: Calling a method, which is defined in another class, from Main class gives Error 1120AS 3.0:从 Main 类调用在另一个类中定义的方法会给出错误 1120
【发布时间】:2010-08-16 02:52:47
【问题描述】:

我有两节课。 Main 类调用一个函数,该函数在 Second 类中定义。我收到以下错误: 错误 1120:未定义属性 myFunction 的访问

基本上,我在 Main 类中创建按钮,将相应的 Child 添加到 Second 类中的 Object (如果单击一个按钮,将添加子 x1,如果单击另一个按钮,将添加子 x2 ,等等)。

这是 Main.as 文件的相关代码:

package
{   
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;         
    public class Main extends MovieClip {
        private var x1:X1 = new X1();
        private var x2:X2 = new X2();

        public function Main():void {
            addPlayers();           
        }

        public function addPlayers():void {
            addChild(x1);
            addChild(x2);
            x1.x=325;
            x1.y=5;
            x2.x=366;
            x2.y=5;

            x1.label = "dog";
            x2.label = "cat";

            x1.addEventListener(MouseEvent.CLICK, selectPlayer);
            x2.addEventListener(MouseEvent.CLICK, selectPlayer);
        }       
    }
}

Second.as 文件代码为:

package  
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.MouseEvent;

    public class Second extends MovieClip
    {
        public var myVar:MyVar = new MyVar();

        public function Second():void
        {   
            addChild(myVar);                        
        }

        private var mc_x1:Mc_x1 = new Mc_x1();
        private var mc_x2:Mc_x2 = new Mc_x2();      

        public function selectPlayer(event:MouseEvent):void
        {
            if (Game(this.parent).turn == 0) {
                myVar.addChild(mc_x1);
            } else {        
            switch (event.currentTarget.label) {
            case "dog":
            myVar.addChild(mc_x1);
            break;
            case "cat":
            myVar.addChild(mc_x2);
            break;
            default:
            myVar.addChild(mc_x1);
            }
            }   
        }   
    }   
}

我尝试将新变量定义为公共静态变量,但没有奏效。我也尝试在 Main 类中导入 Second 类,但这也不起作用。有什么想法吗? 谢谢!

【问题讨论】:

    标签: actionscript-3 variables methods


    【解决方案1】:

    如果我理解你的代码所说的,你就不能做你正在做的事情。即使您正在收听 X1 和 X2 上的事件,您也是在从 main 收听它们。换句话说,main 正在尝试处理该事件,但它没有找到您指定的函数 (selectPlayer)。如果您希望所有事件处理都发生在 main 中,那么 main 在收到事件时必须调用 X1 和 X2。

    【讨论】:

    • 您好 ThatSteveGuy,感谢您的回复!我不明白您所说的“然后 main 在收到事件时必须调用 x1 和 x2”是什么意思。你能澄清一下吗?谢谢!
    • 实际上有很多方法可以完成您正在尝试做的事情,我不确定我是否正确阅读了您的代码,但一种方法是让 eventHandler (selectPlayer) 在main,以及你的播放器类(Second)上的一个公共函数,称为 setAnimal。然后,您在 main 中的事件处理程序将执行以下操作: (event.currentTarget as [YourPlayerClassName]).setAnimal();如果需要,setAnimal 可以带一个参数,指定新的动物。您可能希望在 Second 上侦听事件作为替代方案(如 Widged 建议的那样)。
    【解决方案2】:

    我同意 ThatSteveGuy 的观点,在 Main 中,您正在调用一个不存在的函数,即 selectPlayer()。按照您的代码,您应该做的第一件事是在 Main 中添加一个 selectPlayer() 函数。

    然后您需要在 Main 中添加 Second 类的实例才能使以下工作正常

    private function selectPlayer(event:MouseEvent):void
    {
        second.selectPlayer(event);
    }
    

    现在很难再向您提供任何建议,因为这种做事方式看起来有点复杂,但我还是需要看大局。这只是关于为什么您的代码不起作用的解释。此外,在 Second class 中, selectPlayer 可能会抛出错误,因为 Game(this.parent ) 将返回 null 值,因此您将无法访问 turn 属性...

    您是否有充分的理由需要在 Main 中添加两个按钮而不是在 Second 中添加它们?

    【讨论】:

      【解决方案3】:

      试试这个

       public function Second():void
       {   
                  this.addEventListener(MouseEvent.CLICK, selectPlayer);
                  addChild(myVar);                        
      }
      

      (别忘了从 main 中移除 x1.addEventListener, x2.addEventListener)

      【讨论】:

        猜你喜欢
        • 2016-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-08
        • 2015-08-08
        • 2011-02-02
        • 2023-02-20
        • 2011-10-07
        相关资源
        最近更新 更多