0

我接手了一个项目,我对 as3 真的很陌生!我需要解决一个未解决的问题,而不是从头开始。

我有一个 contaner_mc,有四个 puppet_mc ( puppet_mc1, puppet_mc2, puppet_mc3, puppet_mc4),每个 puppet_mc 有四个按钮 ( buddy_bnt_1, buddy_bnt_2, buddy_bnt_3, buddy_bnt_4)。

我需要听众知道单击了哪个按钮以及单击了哪个木偶?

Object(this).contaner_mc.puppet_mc?.buddy_bnt_?.addEventListener(MouseEvent.CLICK, fl_Click);

function fl_Click(event:MouseEvent):void
{
    gotoAndPlay(15);
}

任何建议将不胜感激!

4

2 回答 2

1

事件冒泡应该是神奇的:

container_mc.addEventListener(MouseEvent.CLICK, click_handler);

function click_handler(event:MouseEvent):void {
  if (event.currentTarget == event.target) {
    return; // the container is the dispatcher
  }

  const sprite:Sprite = event.target as Sprite; // just for a typing
  if (!sprite) {
    trace("event.target is no sprite, set break point here for debugging.");
    return;
  }

  if (sprite.name == 'buddy_bnt_1' || sprite.name == 'puppet_mc1') {
    // do something
  } else if (sprite.name == 'buddy_bnt_2' || sprite.name == 'puppet_mc2') {
    // do something different
  } else if (sprite.name == 'buddy_bnt_3' || sprite.name == 'puppet_mc3') {
    // do something different
  } else if (sprite.name == 'buddy_bnt_4' || sprite.name == 'puppet_mc4') {
    // do something different
  }
}
于 2012-12-29T21:38:22.610 回答
0
container_mc.addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler(event:MouseEvent):void
{
    var clickedObject:DisplayObject = event.target as DisplayObject;

    trace (clickedObject.name);   // name of button clicked
    trace (clickedObject.parent.name);  // name of the parent
}

我使用了 DisplayObject,以便代码可以与 sprite、movieclip 或任何 DisplayObject 一起使用。因此,如果您还没有导入,请确保导入:

import flash.display.DisplayObject;
于 2012-12-30T23:10:24.340 回答