【问题标题】:AS3 How to make only 1 movieclip clickable at a time when there are multiple movieclipsAS3 如何在有多个影片剪辑时一次仅可点击 1 个影片剪辑
【发布时间】:2014-09-23 17:59:08
【问题描述】:

好的,所以我有一个页面,上面有 5 个影片剪辑/按钮。当您将鼠标悬停在每一个上时,它们会亮起(OVER 状态),当您单击它们时,它们会展开(DOWN 状态)。问题是,如果您有多个展开的影片剪辑(处于 DOWN 状态),它们会重叠并且看起来很糟糕。我想对它们进行编码,因此一次只能扩展 1 个。我怎样才能做到这一点?我想我需要在每个按钮上使用 IF 语句,例如“如果任何其他影片剪辑处于 DOWN 状态,则禁用此影片剪辑的 DOWN,如果没有其他按钮处于 DOWN 状态,则启用此影片剪辑的 DOWN 状态”或类似的东西但我不知道怎么写。请帮忙。这是其中一个影片剪辑的代码:

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

好的,我们已经解决了重叠影片剪辑的问题,但是在影片剪辑的 DOWN 状态下,另一个影片剪辑具有以下代码:

Step0img.stop();

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

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

这使我可以单击嵌套的影片剪辑,而无需触发 MOUSE DOWN 并关闭展开的影片剪辑。我认为禁用 MOUSECHILDREN 可能也禁用了此功能。

【问题讨论】:

    标签: actionscript-3 flash button adobe movieclip


    【解决方案1】:

    这是可以做到这一点的一种方法:将上面的代码替换为按钮时间轴上的此代码(或者更好的是制作一个类文件并让每个按钮都将其作为基类,就像我对this question 的回答一样 -那么您只需要拥有所有按钮共享的一个代码文件)

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

    【讨论】:

    • 感谢您的帮助!这在哪里适合上面的代码?就其本身而言,在 Movieclip 的功能中?无论我把它放在哪里,我都会不断收到错误。
    • 什么错误?这将与您发布的其他代码处于相同的上下文中(您可以将其添加到您发布的内容的开头或结尾)
    • 我想这本身不是错误,但如果我将它添加到开头,它会禁用所有按钮。如果我将鼠标悬停在按钮上一次将其添加到末尾,则所有按钮都会被禁用。
    • 哦,我的错,我想我看到了问题,我会更新答案。将代码放在Step0Btn 时间线上。
    • 太棒了,这行得通,但它引起了另一个问题。我将用新问题更新我的问题。
    猜你喜欢
    • 1970-01-01
    • 2018-01-13
    • 2013-07-23
    • 2017-06-24
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多