可以一两个数组,解决这个问题?

时间:2012-12-29 21:17:20

标签: arrays actionscript-3

我接手了一个项目,我对as3真的很新!我希望无需从头开始解决未解决的问题。

我有一个contaner_mc,持有四个puppet_mc(puppet_mc1puppet_mc2puppet_mc3puppet_mc4),每个puppet_mc都有四个按钮(buddy_bnt_1,{ {1}},buddy_bnt_2buddy_bnt_3)。

我需要听众知道点击了什么按钮以及点击该按钮的木偶?

buddy_bnt_4

非常感谢任何建议!

2 个答案:

答案 0 :(得分: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
  }
}

答案 1 :(得分: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;