flash / Actionscript - .alpha 属性并不总是有效



我正在修复别人的代码,一切都在Flash CS5上完成Actionscript 3.0。以下是操作脚本:

var buttons = ["suspend", "dissolve"];
// there are two buttons on the screen. One says 'Suspend' and the other says 'dissolve'.
// When 'suspend' is clicked, information on the word 'suspend' fades in onto the screen
// and, if information on the word 'dissolve' is on the screen, it disappears.
// When 'dissolve' is clicked, information on the word 'dissolve' fades in onto the screen
// and, if information on the word 'suspend' is on the screen, it disappears.
// the code below explains this:
function changeContent (mc:MovieClip):void{
for (var i: int = 0; i < buttons.length ; i++){
    if (buttons[i].associated == mc){ // if 'suspend' or 'dissolve' is clicked
        tweenA = new Tween (buttons[i].associated, "alpha", Strong.easeOut, buttons[i].associated.alpha, 1, tweenSpeed, false); 
        // the line above makes the information corresponding to the button fade in
    }else{
        buttons[i].associated.alpha = 0; // otherwise make the information corresponding to the button disappear
    }
}
checkDone (); // checks if both buttons are clicked.. this part works fine
}

现在,这个动画起作用了,当我点击"暂停"时,关于"暂停"的信息淡入视野,关于溶解的信息消失,反之亦然。但是,如果单击"暂停",然后非常快速地单击"溶解"(如果我一个接一个地单击两个按钮,非常快),那么"暂停"的信息和"溶解"的信息会出现并相互重叠。好像这条线

buttons[i].associated.alpha = 0; // this line is supposed to make the button which
                                 // isn't clicked disappear

如果一个接一个地单击两个按钮,则不起作用,非常快。知道如何解决这个问题吗?

您有一个具有持续时间的补间动画,以及一个用于设置补间属性的语句,因此您会遇到这样的情况:代码的两个部分更改了一个变量,而不知道它在其他地方被更改。解决方案是在设置 alpha 之前停止补间。

    }else{  
        if (tweenA.isPlaying) tweenA.stop(); // there!
        buttons[i].associated.alpha = 0; // otherwise make the information corresponding to the button disappear
    }

但是你需要避免一个变量与多个可以同时变化的对象相关的情况,比如说你希望你的"暂停"和"溶解"关联文本同时淡入(也许在不同的地方),并且你只有一个补间变量,你不能同时控制它们。在这里,我们遇到了同样的事情:使用此代码,除了最后一个按钮之外,您将无法淡入任何文本!这是因为我们对所有可能的补间使用tweenA,即使在特定时间只能有一个活动。因此,我们必须再修复一下。

var tweenB:Tween; // a temporary tween for a new fade in animation
for (var i: int = 0; i < buttons.length ; i++){
    if (buttons[i].associated == mc){ // if 'suspend' or 'dissolve' is clicked
        tweenB = new Tween (buttons[i].associated, "alpha", Strong.easeOut, buttons[i].associated.alpha, 1, tweenSpeed, false); 
        // the line above makes the information corresponding to the button fade in
        // and now we store a new tween in another var so that it won't be stopped at once
    }else{  
        if (tweenA && tweenA.isPlaying) tweenA.stop(); // And here we stop old tween
        // also protecting self from 1009 if no tween was stored yet
        buttons[i].associated.alpha = 0; // otherwise make the information corresponding to the button disappear
    }
}
tweenA=tweenB; // store newly created tween, if any (!) into our current tween var

最新更新