【问题标题】:Button Class by instance name, Actionscript-3按实例名称的按钮类,Actionscript-3
【发布时间】:2010-07-22 00:01:43
【问题描述】:

我太习惯时间线代码了,这让我很困惑。如何获得按钮 类来识别舞台上的按钮实例?
请帮忙。

...刚刚修改
Buttons.fla

Class: 'Buttons'
Button with instance name placed on stage

Buttons.as

package {
    import flash.display.MovieClip;
    public class Buttons extends MovieClip {

         public function Buttons() {
         mc.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
         stage.addChild(this);

         }

         public function onClick(event:MouseEvent):void{
             trace("Hello World");
         }
    }  
}

错误:
1120:未定义的属性
该错误表明它是鼠标事件,而不是我的实例名称 mc。

LINK TO THE FILE

【问题讨论】:

    标签: flash actionscript-3


    【解决方案1】:

    您缺少花括号和 mc 的定义以及 MouseEvent 的导入(上述问题的根源):

    package {
        import flash.display.MovieClip;
        import flash.events.MouseEvent;
    
        public class Buttons extends MovieClip {
    
             public function Buttons() {
                 //it's better to use "this" here instead of adding another
                 //instance of movieclip named "mc"
                 this.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
             }
    
             public function onClick(event:MouseEvent):void{
                 trace("Hello World");
             }
        }  
    }
    

    当然,还有其他几种/更好的方法可以实现这些相同的结果,但这至少可以解决您的编译问题。现在,要在舞台上展示它,您需要将它添加到现有的东西中。一种简单的方法是将以下行放在 this.addEventListener 的正下方:

    stage.addChild(this);
    

    如果您对如何实现此功能还有其他问题,请告诉我。我希望这会为您指明正确的方向,

    --gMale

    编辑:

    回复下面的 cmets 是a link to the Flash files。我试图遵循你正在做的事情的意图。在 IDE 中编码了一个快速可点击按钮,在单独的 *.AS 文件中编码了一个快速可点击按钮:

    【讨论】:

    • 我想为任何可以提供帮助的人提供标记。您的建议会创建 #1046 错误“不是编译时间常数”我需要实例名称才能与我的 fla 的阶段一起使用。
    • @pixelGreaser:上面发布的链接。如果您有任何问题,请告诉我。上面的代码编译得很好。也许您遗漏了 MouseEvent 导入,也许?或者可能 fla 文件仍然指向旧版本的 ActionScript 文件???我不确定。
    【解决方案2】:

    这可能对 'mc' 实例有所帮助

    package {
        import flash.display.MovieClip;
        import flash.events.MouseEvent;
        public class Buttons extends MovieClip {
             public function Buttons() {
               //it's better to use "this" here instead of adding another
               //instance of movieclip named "mc"
             this.addEventListener(MouseEvent.MOUSE_DOWN, onClick); 
             }
             public function onClick(event:MouseEvent):void{
               trace("Hello World");
             //PASS IT INTO THE BRACKETS
             stage.addChild(mc);//<--------------------------
             }
        }  
    }
    

    【讨论】:

      【解决方案3】:

      要访问在 IDE 中创建的实例,您需要使用 [] 语法调用它们,这样可以:

      package {
          import flash.display.MovieClip;
          import flash.events.MouseEvent;
          public class Buttons extends MovieClip {
      
               public function Buttons() {
               this["mc"].addEventListener(MouseEvent.MOUSE_DOWN, onClick);
               //stage.addChild(this); // this is really not useful
               }
               public function onClick(event:MouseEvent):void{
                   trace("Hello World");
               }
          }  
      }
      

      还请注意,您需要导入 MouseEvent。 :)

      如果您真的需要能够通过 mc 访问您的按钮,则需要更多代码:

      package {
          import flash.display.MovieClip;
          import flash.events.MouseEvent;
          public class Buttons extends MovieClip {
      
               protected var mc:MovieClip;
      
               public function Buttons() {
      
                  if(this["mc"] is MovieClip){
                    mc = this["mc"];
                  }else{
                    //you probably want to create it if not found on the stage.
                  }
               mc.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
               }
               public function onClick(event:MouseEvent):void{
                   trace("Hello World");
               }
          }  
      }
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2023-03-16
        • 1970-01-01
        • 2016-03-18
        • 2010-11-13
        • 1970-01-01
        • 1970-01-01
        • 2013-05-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多