在打开另一个影片剪辑之前使影片剪辑不可见的 Actionscript

Actionscript to make movie clip invisible before another is opened

我有一个基本的 Flash 文档,其中显示了几个星期内的足球赛程。我每周都有按钮,例如'Gameweek 1'、'Gameweek 2' 等

按下按钮时,按钮下方会显示一个影片剪辑,其中显示了灯具。 fixtures 影片剪辑始终存在,但按钮将其更改为可见。

我的问题是...如果我有第 1 场比赛的赛程显示,然后我点击 'Gameweek 2' 按钮,两组赛程都会显示,因为第 1 场比赛的赛程仍然可见。

当我按下按钮显示新灯具时,我希望以前可见的影片剪辑现在不可见,以便只有新灯具可见。

这是我的动作脚本:

stop();

btn_game1.addEventListener(MouseEvent.CLICK,openGame1);
function openGame1(evt:MouseEvent) {
if (game1.visible){
    game1.visible = false;

}else {
    game1.visible = true;
}

}

btn_game2.addEventListener(MouseEvent.CLICK,openGame2);
function openGame2(evt:MouseEvent) {

if (game2.visible){
    game2.visible = false;

}else {
    game2.visible = true;
}

}

当你显示 game1 时,你必须隐藏 game2game2.

同样
function openGame1(evt:MouseEvent) {
    if (game1.visible) {
        game1.visible = false;
    }else {
        game1.visible = true;
        game2.visible = false;
    }  
}

我想提出一种重构方法,减少代码量和事件处理程序的数量。

您真的只需要一个按钮处理程序来处理您的所有按钮。您还需要跟踪您 showing/hiding 的所有剪辑。它们可以存储在数组中。

您可以 "connect" 为剪辑指定相似的名称(btn_game1game1)。

以下代码假定您的所有按钮都命名为 btn_gameN,并且您的剪辑都命名为 gameN(其中 N 是一个数字):

var clips:Array = [game1, game2, game3, game4];

btn_game1.addEventListener(MouseEvent.CLICK, onGameButtonClicked);
btn_game2.addEventListener(MouseEvent.CLICK, onGameButtonClicked);
btn_game3.addEventListener(MouseEvent.CLICK, onGameButtonClicked);
btn_game4.addEventListener(MouseEvent.CLICK, onGameButtonClicked);

function onGameButtonClicked(e:MouseEvent):void 
{
    // figure out which button was just clicked by looking at its name
    var clipNum:String = e.currentTarget.name.substr("btn_game".length);

    // loop through all of your clips ...       
    for each(var clip:MovieClip in clips)
    {
        if(clip.name.substr("game".length) == clipNum)
        {
            // if the name matches, toggle the visibility 
            clip.visible = !clip.visible;
        }
        else
        {
            // if the name doesn't match, set visibility to false
            clip.visible = false;
        }
    }
}