【问题标题】:how can Flash understand to call a defined function of a class when the name of the function is not the same as the name of the calss or object?当函数的名称与类或对象的名称不同时,Flash 如何理解调用类的已定义函数?
【发布时间】:2011-05-31 08:13:12
【问题描述】:

在 ActionScript 中,当我为一个对象声明一个类时,我总是为该类和我想要调用的函数使用相同的对象名称。例如,如果对象是 Card,则类名和函数名也是 Card。

但在 AS 游戏编程大学的书中,我第一次看到声明的类与我说的不同。

这本书的一部分是教“如何创建匹配游戏”,他们为游戏声明了两个类。主要课程之一是关于匹配卡片以及我们创建游戏所需的一切。第二课只是为了翻牌,只是为了更漂亮。在第一个中,我们在第二帧中创建一个新符号来调用类及其函数,名称相同。所以当我们到达第二帧时,符号调用它的类,然后调用它的函数。此框架中的显示对象之一是“卡片”。我们需要这些卡片在翻转时翻转,我们不通过为它们创建movieClip来做到这一点,我们只是通过为卡片编写AS来做到这一点。你可以在这里看到actionScript:

我的问题是,当函数名与对象名和类名不同时,Flash会选择播放哪个函数? (这里是“卡”)

package {
    import flash.display.*;
    import flash.events.*;

    public dynamic class Card extends MovieClip {
      private var flipStep:uint;
      private var isFlipping:Boolean = false;
      private var flipToFrame:uint;

      // begin the flip, remember which frame to jump to
      public function startFlip(flipToWhichFrame:uint) {
         isFlipping = true;
         flipStep = 10;
         flipToFrame = flipToWhichFrame;
         this.addEventListener(Event.ENTER_FRAME, flip);
      }

      // take 10 steps to flip
      public function flip(event:Event) {
         flipStep--; // next step
         if (flipStep > 5) { // first half of flip
            this.scaleX = .2*(flipStep-6);
         } else { // second half of flip
            this.scaleX = .2*(5-flipStep);
         }

         // when it is the middle of the flip, go to new frame
         if (flipStep == 5) {
            gotoAndStop(flipToFrame);
         }

         // at the end of the flip, stop the animation
         if (flipStep == 0) {
            this.removeEventListener(Event.ENTER_FRAME, flip);
         }
     }
  }
}

【问题讨论】:

    标签: flash actionscript-3 actionscript


    【解决方案1】:

    当您在 Actionscript 中创建一个类时,您的类有一个称为构造函数的特定函数,它实际上与该类具有相同的名称。

          public class MyExample
          {
              public function MyExample()
              {
                    trace('MyExample constructor!');
              }
          }
    

    当你实例化你的类时,构造函数被调用。

         // trace 'MyExample constructor!'
         private var ex1:MyExample = new MyExample();
    

    你的类可以有公共方法,与你的类构造函数不同,这些方法不会被自动调用

          public class MyExample
          {
              public function MyExample()
              {
                    trace('MyExample constructor!');
              }
    
              public function letsFlipSomeCards():void
              {
                    trace('I like this card game!');
              }
          }
    

    您需要专门调用它们:

        private var ex1:MyExample;
    
        private function init():void
        {
             ex1 = new MyExample(); // trace 'MyExample constructor!'
             ex1.letsFlipSomeCards(); // trace 'I like this card game!'
        }
    

    如果您在此站点上搜索一些关于 AS3 的优秀教程,您会得到很多关于书籍或视频教程的建议,这些建议可以让您对这门语言有一个良好的开端。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      相关资源
      最近更新 更多