ActionSript 3 - 显示鼠标悬停控件

时间:2015-08-12 03:37:28

标签: actionscript-3 flash mouseevent

我目前有一个图形动画,下方有一个简单的播放/暂停按钮,可以停止并启动整个动画:

第1帧:

stop();

btn_2.addEventListener (MouseEvent.CLICK, stopplaying);
function stopplaying(e:MouseEvent):void {
MovieClip(root).stop();
stop();
gotoAndStop(2);
}

第2帧:

stop();

btn_1.addEventListener (MouseEvent.CLICK, startplaying);
function startplaying(e:MouseEvent):void {
MovieClip(root).play();
play();
gotoAndStop(1);
}

这简单而完美。但是,我希望控制按钮显示在鼠标悬停时,当鼠标离开动画区域时再次变为透明。简单地将alpha状态映射到鼠标事件有效,但似乎也打破了按钮的功能。任何帮助都将非常感激!

更新: @BadFeelingAbout这有很好的逻辑,但我没有取得多大成功。要清楚,现在我的场景动作的第1帧是:

var btn_1, btn_2;

this.addEventListener(MouseEvent.MOUSE_OVER, mouseOver);
this.addEventListener(MouseEvent.MOUSE_OUT, mouseOut);

function mouseOver(e:Event):void {
if(btn_1) btn_1.visible = true;
if(btn_2) btn_2.visible = true;
}

function mouseOut(e:Event):void {
if(btn_1) btn_1.visible = false;
if(btn_2) btn_2.visible = false;
}

该按钮已隐藏,但在鼠标悬停时不再出现。我能看到的唯一失败点是关键字'this',即我正在错误地使用它。如果我能提供任何其他信息,请告诉我们!

更新2 :更多信息(我在这里为我的朦胧道歉):这是动画:[链接剪切,更新下面的链接]。播放/暂停按钮是一个名为“pp”的影片剪辑,其中包含两个框架,每个框架都有一个按钮,一个名为btn_1,另一个名为btn_2。

更新3 :我添加了一个透明背景方块(名为“backpp”)作为mouseevent区域(而不是使用更广泛的“this”):

backpp.addEventListener(MouseEvent.MOUSE_OVER, mouseOver);
backpp.addEventListener(MouseEvent.MOUSE_OUT, mouseOut);

这很棒!当鼠标悬停在方块上时,控件显示出来。当我鼠标移开时,它们会消失。但是,播放/暂停功能现在无法正常工作。有任何想法吗?

更新4 :下面的最新代码/上下文。播放/暂停按钮现在具有多种功能,并且按预期隐藏,但是呈现出视觉上的“闪烁”行为,如下所示:http://allaboarddesign.com/rodney/rodney-test.swf

以下是我的FlashPro上下文的屏幕截图:enter image description here

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

//create placeholder vars for your btns (they will be populated by the instances on the timeline)
//do this so the compiler knows they exist
var btn1, btn2; 

//hide the buttons, do this on frame 2 as well but with btn2
btn1.visible = false;

//listen for mouse over/out on `this` (the timeline whose code this on, presumably your animation)
this.addEventListener(MouseEvent.MOUSE_OVER, mouseOver);
this.addEventListener(MouseEvent.MOUSE_OUT,mouseOut);

function mouseOver(e:Event):void {
    //show the buttons if they exist
    if(btn1) btn1.visible = true;
    if(btn2) btn2.visible = true;
}

function mouseOut(e:Event):void {
    //hide the buttons if they exist
    if(btn1) btn1.visible = false;
    if(btn2) btn2.visible = false;
}

修改

根据您的屏幕截图,它看起来您的层次结构是这样的:

Maintimeline -> pp -> btn1

pp是主时间轴动画的控件。

在这种情况下,代码应该在Main Timeline上,如下所示:

pp.visible = false;

//listen for mouse over/out on `this` (the timeline whose code this on, presumably your animation)
this.addEventListener(MouseEvent.MOUSE_OVER, mouseOver);
this.addEventListener(MouseEvent.MOUSE_OUT,mouseOut);

function mouseOver(e:Event):void {
    //show the buttons if they exist
    pp.visible = true;
}

function mouseOut(e:Event):void {
    //hide the buttons if they exist
    pp.visible = false;
}

要让鼠标停止工作,你的动画将需要在背景中鼠标悬停,甚至透明形状或movieClip也可以。