AS3如何使只有1影片剪辑可点击的时候有多个影片剪辑 [英] AS3 How to make only 1 movieclip clickable at a time when there are multiple movieclips

查看:159
本文介绍了AS3如何使只有1影片剪辑可点击的时候有多个影片剪辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,我有上5影片剪辑/按钮的页面。当你的鼠标移到每一个,他们点亮(悬停状态),当你点击他们,他们扩大(向下状态)。现在的问题是,如果你有扩大(在DOWN状态)它们重叠多个影片剪辑,它看起来可怕。我想code所以他们只有1个可以同时展开。我怎样才能做到这一点?我想我需要在每个按钮的IF语句,如如果任何其他影片剪辑处于停机状态,然后禁用下来这个影片剪辑,如果没有其他按钮处于向下状态,则启用向下状态,这个影片剪辑或类似的东西但我不知道怎么写。请帮忙。这里是code为影片剪辑中的一个:

Ok, so I have a page with 5 movieclips/buttons on it. When you mouse over each one they light up (OVER state) and when you click on them they expand (DOWN state). The problem is if you have multiple movieclips expanded (in the DOWN state) they overlap and it looks awful. I want to code them so only 1 can be expanded at a time. How can I do this? I imagine I need an IF statement on each button like "If any other movieclips are in the DOWN state, then disable DOWN for this movieclip, if no other buttons are in DOWN state then enable the DOWN state for this movieclip" or something like that but I don't know how to write it. Please help. Here is the code for one of the movieclips:

Step0btn.stop();

Step0btn.addEventListener(MouseEvent.MOUSE_DOWN, onStep0Press);
Step0btn.addEventListener(MouseEvent.MOUSE_OVER, onStep0Over);
Step0btn.addEventListener(MouseEvent.MOUSE_OUT, onStep0Out);

function onStep0Press(event:MouseEvent):void
{
    // toggle between frame 1 and 3 on button press
    Step0btn.gotoAndStop(Step0btn.currentFrame == 3 ? 1 : 3);
}

function onStep0Over(event:MouseEvent):void
{

    if (Step0btn.currentFrame != 3)

    {

        Step0btn.gotoAndStop(2);

    }

}

function onStep0Out(event:MouseEvent):void
{
    // only switch back to UP state if the button is "pressed"
    if (Step0btn.currentFrame != 3)
    {
        Step0btn.gotoAndStop(1);
    }
} 

好了,我们已经解决了这个问题,重叠影片剪辑,但是在影片剪辑的向下状态还有另外一个影片剪辑具有这种code:

Ok we have solved the issue with the overlapping movieclips, however on the movieclip's DOWN state there is another movie clip that has this code:

Step0img.stop();

Step0img.addEventListener(MouseEvent.MOUSE_DOWN, onStep0imgPress, false, 999);

function onStep0imgPress(event:MouseEvent):void
{
    Step0img.gotoAndStop(2);
    event.stopImmediatePropagation();
}

这让我点击嵌套的影片剪辑,而不会触发鼠标按下甚至闭展开影片剪辑。我认为禁止将mouseChildren可能也禁用此功能。

This allowed me to click on the nested movieclip without triggering the MOUSE DOWN even and closing the expanded movieclip. I think disabling the MOUSECHILDREN may have also disabled this functionality.

推荐答案

这里有一种方法可以做到这一点:更换code以上这个code对您的按钮时间表(或更好,但使一个类每个文件和你的按钮有它像我的回答<一其基类href="http://stackoverflow.com/questions/23351096/as3-multiple-rollover-objects/23355106#23355106">this问题 - 那么你只需要拥有一个code文件,所有的按钮股)

Here is one way this can be done: Replace the code above with this code on your button timelines (or better yet make a class file and have your buttons each have it as their base class like my answer to this question - then you only need to have the one code file that all your buttons share)

stage.addEventListener(MouseEvent.MOUSE_DOWN,globalMouseDown,false,0,true); //add a global mouse listener

function globalMouseDown(e:Event):void {
    //find out if the target is a descendant of this, if not, then something else was clicked.
    var tmpParent:DisplayObject = e.target as DisplayObject;
    while(tmpParent && tmpParent != stage){
        if(tmpParent == this) return;
        tmpParent = tmpParent.parent;
    }

    //something else was clicked that wasn't this, so go to the up state
    gotoAndStop(1);

}

stop();

addEventListener(MouseEvent.MOUSE_DOWN, onPress);
addEventListener(MouseEvent.MOUSE_OVER, onOver);
addEventListener(MouseEvent.MOUSE_OUT, onOut);

function onPress(event:MouseEvent):void
{
    // toggle between frame 1 and 3 on button press
    gotoAndStop(Step0btn.currentFrame == 3 ? 1 : 3);
}

function onOver(event:MouseEvent):void
{

    if (currentFrame != 3)
    {
        gotoAndStop(2);
    }
}

function onOut(event:MouseEvent):void
{
    // only switch back to UP state if the button is "pressed"
    if (currentFrame != 3)
    {
        gotoAndStop(1);
    }
} 

这篇关于AS3如何使只有1影片剪辑可点击的时候有多个影片剪辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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