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

时间:2014-01-14 18:25:39

标签: actionscript-3 flash alpha-transparency

我正在修复其他人的代码,所有工作都在Flash CS5上运行。这是动作脚本:

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
如果两个按钮一个接一个地点击,

不起作用,非常快。知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

你有一个具有持续时间的补间动画和一个用于设置补间属性的语句,所以当你的代码的两个部分改变一个变量而不知道它在其他地方被改变时,你会遇到这种情况。解决方案是在设置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