【问题标题】:Action Script 3.0. How to access MovieClip object from another class?动作脚本 3.0。如何从另一个类访问 MovieClips 对象?
【发布时间】:2014-06-19 01:23:31
【问题描述】:

所以我有 2 个类 MainEnemy。在 Enemy 类中,我需要使用在 Main 类中声明的变量。

这是我的 Main 类:

   public class Script extends MovieClip {
       var hero:MovieClip;
       var enemy:MovieClip; //These variables are only for example

       public function Game() {
                   hero.x = 100;
                   enemy.x = 200; // that's only example
   }
       function collisionDetected() {
                   enemy.hitBack(); // this is how I call hitBack function from Enemy class
   }

}

这是我的Enemy类:

public class Enemy extends MovieClip {
        private var count = 0;

        public function hitBack() {
            count = 0;
            this.addEventListener(Event.ENTER_FRAME, myEnterFrame);
        }

    private function myEnterFrame(e:Script)
    {
       if (count == 20) this.removeEventListener(Event.ENTER_FRAME, myEnterFrame);
       else 
       {
          count++;
          if (hero.x < enemy.x) { //here I need to use variables from Main class
            this.x -= 4;  
          }
           else {
            this.x += 4;  

          }
       }

    }

我收到以下错误

1120: Access of undefined property hero.
1120: Access of undefined property enemy.

【问题讨论】:

    标签: actionscript-3 flash class actionscript movieclip


    【解决方案1】:

    您需要将该计算移到 Main 类,或者您也可以将变量传递给敌人类。由于您的示例已被精简,我不确定在您的情况下什么是最好的,但我倾向于将计算移出,假设您有多个敌人试图这样做。

    public class Script extends MovieClip {
           var hero:MovieClip;
           var enemy:MovieClip; //These variables are only for example
    
           public function Game() {
                       hero.x = 100;
                       enemy.x = 200; // that's only example
                       this.addEventListener(Event.ENTER_FRAME, enterFrame);
           }
    
           function collisionDetected() {
                       enemy.hitBack(); // this is how I call hitBack function from Enemy class
           }
    
           function enterFrame(e:Event){
                theEnemyMovieclipClass.myEnterCheck(hero,enemy);
           }
    
    
    
    
    public class Enemy extends MovieClip {
            private var count = 0;
    
            public function hitBack() {
                count = 0;
            }
    
        public function myEnterCheck(hero,enemy)
        {
           if (count == 20) {
                // DO SOMETHING
           }
           else 
           {
              count++;
              if (hero.x < enemy.x) { //here I need to use variables from Main class
                this.x -= 4;  
              }
               else {
                this.x += 4;  
    
              }
           }
    
        }
    }
    

    这样,如果你有多个,你可以遍历所有这些并调用相同的方法 onEnterFrame。

    【讨论】:

      猜你喜欢
      • 2011-04-18
      • 2013-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多