【问题标题】:AS3 movieclip buttons inside of movieclip buttons影片剪辑按钮内的 AS3 影片剪辑按钮
【发布时间】:2014-07-29 21:38:27
【问题描述】:

我有一个与鼠标操作相关的影片剪辑。当您单击影片剪辑时,图像会展开以显示一些文本和另一个图像。我需要能够单击该新图像并将其展开,但我无法单击它,因为我已经编写了初始影片剪辑以通过鼠标单击打开和关闭。我怎样才能忽略最初的鼠标点击?我一直在寻找答案,并想出了我无法开始工作的 mousechildren,并将按钮放在另一层,但我似乎无法理解它。

这是初始影片剪辑的代码:

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]

【问题讨论】:

  • 您能解释一下您的步进按钮的层次结构吗?从您的错误和代码来看,它看起来像这样:MainTimeline -> step0_btn_7 -> Step0btn -> Step0img 对吗?你确定你所有的实例名称拼写正确吗?您更新后的代码与原始发布的代码之间存在区分大小写的差异。

标签: actionscript-3 flash button adobe movieclip


【解决方案1】:

您可以通过以下两种方式完成您想要的工作。

  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 之类的操作来防止他们成为目标。

  2. 侦听具有更高优先级的图像,然后取消事件,这样它就不会冒泡到父级。

    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.
    }
    

【讨论】:

  • 我不断收到我不明白的“未定义属性 step0btn 的访问”错误。它已定义,我已经编写了代码。我早些时候收到了这个错误,它只发生在我尝试为movieclip INSIDE step0btn 编写代码时。那么 step0btn.step0img 有什么问题呢?就像当我在 step0img 中时,它不再识别 step0btn,即使那是它所在的位置。
  • 我认为在 Step0btn 之后添加 (parent) 或 (root) 可能有效,但不行。
  • 所以,只是为了确认 - step0imgstep0btn 的时间线上?另外,step0img 出现在step0btn 时间线的每一帧上?可能是 step0img 在抛出错误时在 step0Btn 所在的任何帧上都不存在。
  • 我可以使用 "this.step0btn" 来获取它,但整个过程还不能正常工作,我必须再做一些调整。如果我不能让它工作,我会回复,看看你是否能提供进一步的帮助。
  • 太棒了!我的任何一个例子都帮助你解决了你的问题吗?如果是,请接受答案,如果不是,请用您当前的绊脚石更新您的问题。如果您发现问题的完全不同的解决方案,请将其作为答案发布。
猜你喜欢
  • 2018-03-29
  • 1970-01-01
  • 1970-01-01
  • 2018-01-13
  • 2011-12-29
  • 1970-01-01
  • 2013-06-09
  • 1970-01-01
  • 2014-04-03
相关资源
最近更新 更多