AS3多个侧翻对象 [英] AS3 Multiple Rollover objects

查看:119
本文介绍了AS3多个侧翻对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如图片

很新的AS3。很抱歉,如果这个问题是非常基本的,我试图四处寻找正确的答案,但只找到半相关的问题。请帮助!

目标:我要同台的多个侧翻影片剪辑可以独立发挥出自己的动画

到目前为止,我只有1 MovieClip对象,其行为正常。如果我添加另一个,第一个表现正常,但第二次没有出现在所有。据我所知,它可能只是打电话,我第一次投进阶段,我需要改变我的code有一个大师或父影片剪辑和实例应该是孩子们的情况,但我不知道如何写,在code。最终,这个想法是,我想补充我的孩子影片剪辑,然后略微改变在每个片段的内容。

我的code到目前为止:

 进口flash.events.MouseEvent;


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


功能clipOver(事件:MouseEvent)方法:无效{

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

};

功能clipOut(事件:MouseEvent)方法:无效{

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

};
 

解决方案

有几个方法可以做到这一点。我会以最坏的最好列出。

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

    当你将一个新的影片剪辑到时间线,你需要给它一个实例名称(在属性面板中找到)。我不知道,如果 clip_boxes 的是,你打算把所有的电影剪辑上,或父母的时间表,如果它是你的影片剪辑本身之一。

    假设你有3剪辑实例名称: MC1 MC2 MC3 ,你可以这样做

    (包含它们的时间轴的第一帧)

      MC1.addEventListener(MouseEvent.ROLL_OVER,clipOver);
    MC2.addEventListener(MouseEvent.ROLL_OVER,clipOver);
    MC3.addEventListener(MouseEvent.ROLL_OVER,clipOver);
    
    //如果你有一大堆,你也可以使用一个循环来添加所有监听器
    
    
    //你用event.currentTarget获得referce对象的侦听器附加到 - 这种方式,你只需要这一个处理函数
    功能clipOver(事件:MouseEvent)方法:无效{
        影片剪辑(event.currentTarget).addEventListener(MouseEvent.ROLL_OUT,clipOut);
        影片剪辑(event.currentTarget).gotoAndPlay(在);
    };
    
    功能clipOut(事件:MouseEvent)方法:无效{
        影片剪辑(event.currentTarget).removeEventListener(MouseEvent.ROLL_OUT,clipOut);
        影片剪辑(event.currentTarget).gotoAndPlay(外出);
    };
     

  2. 使用继承

    这将涉及建立一个基础类文件(。至于文件),然后可以连接到您的所有影片剪辑,使他们继承了所有内code。这里是一个类文件,它会为你做这个的例子:(让我们假设这是一个名为您的根目录SubClass.as文件)

     包{
        进口的flash.display.MovieClip;
    进口flash.events.MouseEvent;
    
        公共类子类来扩展MovieClip的{
            公共职能SubClass中(){
                this.addEventListener(MouseEvent.ROLL_OVER,侧翻,假,0,真正的);
            }
    
            公共职能翻转(事件:MouseEvent)方法:无效{
                this.addEventListener(MouseEvent.ROLL_OUT,部署,假,0,真正的);
                this.gotoAndPlay(OVER);
            }
    
            公共职能部署(事件:MouseEvent)方法:无效{
                this.removeEventListener(MouseEvent.ROLL_OUT,部署,假);
                this.gotoAndPlay(出);
            }
        }
    }
     

    现在,当您创建的影片剪辑(或右键单击库中,选择属性),你可以设置一个基类他们。如果您设置的基类的类上面,它们会自动使用code以上,并具有鼠标输入/输出连接。 (只要他们有输出/过帧标签将只是工作)。

example image

Very new to AS3. Sorry if this question is really basic, I tried looking around for the right answer but only found semi-related questions. Please help!!

Objective: I want multiple rollover MovieClips on the same stage that play out their animations independently.

So far, I have only 1 MovieClip object that behaves properly. If I add another, the first one behaves properly but the second doesn't appear at all. I understand that it's probably only calling the instance that I first dropped into the stage and that I need to change my code to have a "master" or parent MovieClip and that the instances should be the children, but I'm not sure how to write that in code. Eventually, the idea is that I add my children movieclips, and then slightly change the content in each clip.

My code so far:

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");

};

解决方案

There are a few ways you can do this. I'll list in order of worst to best.

  1. Manually add listeners to each instance.

    When you drag a new MovieClip onto the timeline, you need to give it an instance name (found in the properties panel). I'm not sure if clip_boxes is a parent timeline that you intend to have all your movie clips on, or if it is one of your movie clips itself.

    Assuming you have 3 clips with the instance names: MC1,MC2,MC3, you could do this (on the first frame of the timeline that contains them)

    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. Use Inheritance

    This would involve creating a base class file (.as file) that you can then attach to all your MovieClips so they inherit all the code within. Here is an example of a class file that would do this for you: (lets assume this is a file called SubClass.as in your root directory)

    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");
            }
        }
    }
    

    Now, when you create your movieClips (or right click in the library and select properties), you can set a baseClass for them. If you set the base class to the class above, they automatically use the code above and have the mouse over/out attached. (as long as they have the Out/Over frame labels it will just work).

这篇关于AS3多个侧翻对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆