我有一个与某些鼠标动作相关的影片剪辑。单击影片剪辑时,图像会展开以显示一些文本和另一个图像。我需要能够点击那个新图像并进行扩展,但是我无法点击它,因为我已经编写了初始影片剪辑,只需点击鼠标即可打开和关闭。如何忽略鼠标初始点击?我一直在寻找答案,并且已经想出了我无法上班的鼠标,也把按钮放在另一层上,但我似乎无法绕过它。
以下是初始影片剪辑的代码:
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);
}
}
然后在里面,我有另一个带有此代码的影片剪辑:
step0img.stop();
step0img.addEventListener(MouseEvent.MOUSE_DOWN, onStep0imgPress);
function onStep0imgPress(event:MouseEvent):void
{
step0img.gotoAndStop(1);
}
由于对初始动画片段进行编码,因此完全忽略了此部分。我怎么能改变这个?
更新
好的,这是新代码:
Step0img.stop();
Step0img.addEventListener(MouseEvent.MOUSE_DOWN, onStep0imgPress);
function onStep0imgPress(event:MouseEvent):void
{
if(event.target == this.Step0btn.Step0img){
//the click was actually the image
this.Step0btn.Step0img.gotoAndStop(1);
}else{
// toggle between frame 1 and 3 on button press
this.Step0btn.gotoAndStop(this.Step0btn.currentFrame == 2 ? 1 : 2);
}
}
当我调试并且电影播放正常时它没有给我任何错误,除非我点击我刚编码的按钮。它不起作用,我收到此错误:
TypeError: Error #1010: A term is undefined and has no properties.
at SMARTTTraining_fla::step0_btn_7/onStep0imgPress()[SMARTTTraining_fla.step0_btn_7::frame3:7]
答案 0 :(得分:1)
以下两种方法可以实现您的目标。
只需在父项上使用单击侦听器并检查事件的目标(target
是最后一个被点击的向下显示对象,而不是currentTarget
侦听器附加到的对象)
function onStep0Press(event:MouseEvent):void
{
if(event.target == step0btn.step0img){
//the click was actually the image
step0btn.step0img.gotoAndStop(1);
}else{
// toggle between frame 1 and 3 on button press
step0btn.gotoAndStop(step0btn.currentFrame == 3 ? 1 : 3);
}
}
关于这种方法需要注意的一点是,如果step0img
有孩子,他们可能是事件的目标。您可能需要执行step0img.mouseChildren = false
之类的操作以防止它们成为目标。
聆听具有更高优先级的图像,然后取消该事件,使其不会冒泡到父母。
step0img.addEventListener(MouseEvent.MOUSE_DOWN, onStep0imgPress, false, 999); //the 999 is an arbitrary number, listeners with a higher priority will be handled first, if they have the same priority (default is 0), then they will be handled backwards from order the listeners were added.
function onStep0imgPress(event:MouseEvent):void
{
step0img.gotoAndStop(1);
event.stopImmediatePropagation(); //this will stop the event from triggering any other listeners for it.
}