【问题标题】:AS3 Multiple Rollover objectsAS3 多个翻转对象
【发布时间】:2014-04-28 21:00:59
【问题描述】:

example image

对 AS3 非常陌生。抱歉,如果这个问题真的很基本,我试着四处寻找正确的答案,但只找到了半相关的问题。请帮忙!!

目标:我希望在同一个舞台上独立播放动画的多个翻转影片剪辑。

到目前为止,我只有 1 个行为正常的 MovieClip 对象。如果我添加另一个,第一个行为正常,但第二个根本不出现。我知道它可能只是调用我第一次进入舞台的实例,并且我需要更改我的代码以拥有一个“主”或父 MovieClip,并且这些实例应该是孩子,但我不知道如何用代码写出来。最终,我的想法是添加我的子影片剪辑,然后稍微更改每个剪辑中的内容。

到目前为止我的代码:

import flash.events.MouseEvent;


clip_boxes.removeEventListener(MouseEvent.ROLL_OUT, clipOut);
clip_boxes.addEventListener(MouseEvent.ROLL_OVER, clipOver);


function clipOver(event:MouseEvent):void {

 clip_boxes.addEventListener(MouseEvent.ROLL_OUT, clipOut);
 clip_boxes.removeEventListener(MouseEvent.ROLL_OVER,clipOver);
 clip_boxes.gotoAndPlay("Over");

};

function clipOut(event:MouseEvent):void {

clip_boxes.addEventListener(MouseEvent.ROLL_OVER, clipOver);   
clip_boxes.removeEventListener(MouseEvent.ROLL_OUT, clipOut);
clip_boxes.gotoAndPlay("Out");

};

【问题讨论】:

    标签: actionscript-3 actionscript instance parent-child rollover


    【解决方案1】:

    有几种方法可以做到这一点。我会按照从最差到最好的顺序列出。

    1. 手动为每个实例添加监听器。

      当您将新的影片剪辑拖到时间轴上时,您需要为其指定一个实例名称(可在属性面板中找到)。我不确定clip_boxes 是否是您打算包含所有影片剪辑的父时间轴,或者它是否是您的影片剪辑本身之一。

      假设您有 3 个片段的实例名称为:MC1MC2MC3,您可以这样做(在包含它们的时间线的第一帧上)

      MC1.addEventListener(MouseEvent.ROLL_OVER, clipOver);
      MC2.addEventListener(MouseEvent.ROLL_OVER, clipOver);
      MC3.addEventListener(MouseEvent.ROLL_OVER, clipOver);
      
      //If you had a whole bunch, you could also use a loop to add all the listeners
      
      
      //you use event.currentTarget to get a referce to the object the listener was attached to - this way you only need this one handler function
      function clipOver(event:MouseEvent):void {
          MovieClip(event.currentTarget).addEventListener(MouseEvent.ROLL_OUT, clipOut);
          MovieClip(event.currentTarget).gotoAndPlay("Over");
      };
      
      function clipOut(event:MouseEvent):void {  
          MovieClip(event.currentTarget).removeEventListener(MouseEvent.ROLL_OUT, clipOut);
          MovieClip(event.currentTarget).gotoAndPlay("Out");
      };
      
    2. 使用继承

      这将涉及创建一个基类文件(.as 文件),然后您可以将其附加到所有 MovieClip,以便它们继承其中的所有代码。下面是一个类文件的示例,可以为您执行此操作:(假设这是您根目录中名为 SubClass.as 的文件)

      package {
          import flash.display.MovieClip;
      import flash.events.MouseEvent;
      
          public class SubClass extends MovieClip {
              public function SubClass(){
                  this.addEventListener(MouseEvent.ROLL_OVER, rollOver,false,0,true);
              }
      
              public function rollOver(event:MouseEvent):void {
                  this.addEventListener(MouseEvent.ROLL_OUT,rollOut,false,0,true);
                  this.gotoAndPlay("Over");
              }
      
              public function rollOut(event:MouseEvent):void {
                  this.removeEventListener(MouseEvent.ROLL_OUT,rollOut,false);
                  this.gotoAndPlay("Out");
              }
          }
      }
      

      现在,当您创建movieClips(或右键单击库并选择属性)时,您可以为它们设置一个baseClass。如果将基类设置为上面的类,它们会自动使用上面的代码并附加鼠标悬停/移出。 (只要他们有 Out/Over 框架标签,它就可以工作)。

    【讨论】:

    • 首先,非常感谢。其次,我尝试实现详细的继承方法,但它不起作用。它抛出的错误是:SubClass.as, Line 10, Column 40 1046: Type was not found or is not a compile-time constant 思考?我想我正确设置了链接,我什至尝试调用“MC1.SubClass();”在主时间线动作层中。没运气。有什么我想念的吗?
    • 这可能是因为其中一个导入语句是错误的。 import Events.MouseEvent 实际上应该是 import flash.events.MouseEvent - 我更新了答案。
    【解决方案2】:

    您还可以发布将剪辑框添加到舞台的代码吗?您是通过拖放在 GUI 中添加它们还是在代码中添加它们?

    如果是这样,您可能需要在包含所有剪辑框的较大影片剪辑中创建每个剪辑框的实例。然后你需要用clip_boxes.box1等来引用每一个。

    编辑: 哦,我看到你那里有一张图片。我的错。确保为每个剪辑框赋予其自己唯一的实例名称。您需要有 clip_box_1、clip_box_2 等。然后在代码中使用 clip_box_1.addEventListen .....等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多