好的,所以我有一个页面上有5个动画片段/按钮。当你将鼠标悬停在每一个上时它们会亮起(OVER状态),当你点击它们时它们会展开(DOWN状态)。问题是如果你有多个动画片段被展开(处于DOWN状态)它们重叠并且看起来很糟糕。我想对它们进行编码,因此一次只能扩展1个。我怎样才能做到这一点?我想我需要在每个按钮上使用IF语句,例如"如果任何其他动画片段处于DOWN状态,则对此动画片段禁用DOWN,如果没有其他按钮处于DOWN状态,则为此动画片段启用DOWN状态&#34 ;或类似的东西,但我不知道如何写它。请帮忙。以下是其中一个动画片段的代码:
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);
}
}
好的,我们已经解决了重叠的动画片段的问题,但是在movieclip的DOWN状态下,还有另一个具有此代码的影片剪辑:
Step0img.stop();
Step0img.addEventListener(MouseEvent.MOUSE_DOWN, onStep0imgPress, false, 999);
function onStep0imgPress(event:MouseEvent):void
{
Step0img.gotoAndStop(2);
event.stopImmediatePropagation();
}
这使我可以点击嵌套的动画片段而不会触发MOUSE DOWN甚至关闭扩展的动画片段。我认为禁用MOUSECHILDREN可能也禁用了此功能。
答案 0 :(得分:1)
以下是一种可以做到的方法:在按钮时间轴上用上面的代码替换上面的代码(或者更好地创建一个类文件,让每个按钮都将它作为基类,就像我对{{3}的回答一样 - 那么你只需要拥有所有按钮共享的一个代码文件)
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);
}
}