【问题标题】:ActionSript 3 - show controls on mouseoverActionSript 3 - 在鼠标悬停时显示控件
【发布时间】:2015-08-12 03:37:28
【问题描述】:

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

第一帧:

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 状态映射到鼠标事件是可行的,但似乎也破坏了按钮的功能。任何帮助将不胜感激!

更新: @BadFeelingAboutThis 有很好的逻辑,但我并没有取得太大的成功。需要明确的是,我的场景动作的第 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”)作为鼠标事件区域(而不是使用更广泛的“this”):

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

这很好用!当我将鼠标悬停在正方形上时,控件就会出现。当我鼠标移出时,它们就消失了。但是,播放/暂停功能现在不起作用。有任何想法吗?

更新 4:以下最新代码/上下文。播放/暂停按钮现在可以正常工作,并且按预期隐藏,但呈现出视觉“闪烁”行为,如下所示:http://allaboarddesign.com/rodney/rodney-test.swf


这是我的 FlashPro 上下文的屏幕截图:

【问题讨论】:

  • 闪烁行为是因为错误。您的代码在 pp 时间线上(这意味着 pp 对象在该上下文中为 this)。因此,当您执行var pp 时,您在pp 中声明了一个名为pp 的新变量,它没有任何值,因此在您尝试执行pp.visible 时会引发错误
  • @BadFeelingAboutThis 删除 'var' 声明并将 'addEventListener' 行从 'backpp' 指向 'this' 让我几乎到了那里 - 现在闪烁仅处于一种状态 [上面更新的链接],并且看起来更干净。我敢肯定,我错过了最后一件(显而易见的)事情。 (另外,非常感谢您非常对我缺乏经验的耐心等待。我的设计师帽子正在展示。)
  • 您正在使用我更新答案中的代码? (代码在主时间线上?)你有其他时间线上的代码吗?同样,闪烁通常意味着 flashees 时间轴上有错误,因此您的播放/暂停按钮上可能有一些错误代码。
  • 你解决了吗?

标签: actionscript-3 flash mouseevent


【解决方案1】:

您可以执行以下操作:

//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;
}

要使鼠标悬停在上面,您的动画需要在背景中放置一些东西才能将鼠标悬停,即使是透明的形状或影片剪辑也可以。

【讨论】:

  • 顺便说一句,play() 后面跟着gotoAndStop() 是没有意义的
  • 更新了 OP 并提供了更多信息。这看起来不错,但我还没有找到成功。详情如上。还有,谢谢!
  • 为此,必须有一些鼠标悬停在上面。我假设当前上下文(此代码所在的位置)不仅仅是 btn1?
  • 更多信息(我为我的晦涩道歉):这里是动画:link。播放/暂停按钮是一个名为“pp”的影片剪辑,它包含两帧,每帧都有一个按钮,一个名为 btn_1,另一个名为 btn_2。
  • 所以您的影片剪辑层次结构看起来像这样? MainTimeline -> Animation -> pp -> btn_1 并且您显示的代码在 pp 时间线上?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 2012-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多