AS3多个滚动对象

时间:2014-04-28 21:00:59

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

example image

AS3很新。对不起,如果这个问题非常基本,我试着寻找正确的答案,但只找到了半相关的问题。请帮忙!!

目标:我希望在同一个舞台上多个翻转MovieClip,以便独立播放动画。

到目前为止,我只有一个表现正常的MovieClip对象。如果我添加另一个,第一个表现正常,但第二个表现不正常。我知道它可能只调用我第一次进入舞台的实例,并且我需要更改我的代码以拥有一个" master"或者父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");

};

2 个答案:

答案 0 :(得分:2)

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

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

    将新的MovieClip拖到时间轴上时,需要为其指定实例名称(在属性面板中找到)。我不确定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帧标签,它就可以工作)。 Here is a screen shot of how you link up the class with the movie clip

答案 1 :(得分:0)

您是否也可以将代码添加到舞台上的代码?您是通过拖放还是在代码中将它们添加到GUI中?

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

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