Action Script 3.0 - MouseEvent侦听器+功能问题

时间:2015-02-09 17:46:14

标签: actionscript-3 flash actionscript mouseevent

我正在开发一个在舞台上有多个可点击对象的项目。

但是,当我创建以下函数时,我得到了最后定义的eventlistener。

// Event listeners
antwoordBox1.addEventListener(MouseEvent.CLICK, antwoordboxclick);
antwoordBox2.addEventListener(MouseEvent.CLICK, antwoordboxclick);

function antwoordboxclick(event:MouseEvent):void {

    trace (event.currentTarget.name); // traces 'antwoordBox2' , whether I click any of the buttons.

    if (event.currentTarget.name == 'antwoordBox1') {

         trace('antwoordBox1 selected');

    }

    if (event.currentTarget.name == 'antwoordBox2')  {

        trace('antwoordBox2 selected');
    }

    currentQuestion++; // function to handle after above

    trekNieuweVraag(); // another function to handle after above
}
// end of my code

无论我尝试什么,我都不会解决它。当我使用event.target时,我在antwoordBox2(或antwoordBox1)中获得子动画片名称。

希望有人能帮助我! :)

1 个答案:

答案 0 :(得分:0)

  1. 禁止与antwoordBox的孩子进行鼠标互动:

    antwoordBox1.mouseChildren = false;
    antwoordBox2.mouseChildren = false;
    
  2. 注册事件监听器:

    addEventListener(MouseEvent.CLICK, antwoordboxclick);
    
  3. 在事件处理程序中:

    if (event.target == antwoordBox1)
        trace('antwoordBox1 selected');
    else if (event.target == antwoordBox2)
        trace('antwoordBox2 selected');