如何获取动态创建的Flash舞台上MovieClip的名称?

发布于 2024-09-14 10:36:20 字数 597 浏览 4 评论 0原文

有多个影片剪辑将动态放置在舞台上。这些影片剪辑被编码为按钮。我试图弄清楚——当用户单击 MovieClip 时...找出用户单击了 Flash 舞台上的哪个对象。

在函数toggleClick 内部,我放置了跟踪语句:

trace("movieClip Instance Name = " + e.target.name);

在输出窗口中:

movieClip Instance Name = instance5 
movieClip Instance Name = instance12 
movieClip Instance Name = instance5 
movieClip Instance Name = instance32 
movieClip Instance Name = instance5 
movieClip Instance Name = instance59 

这似乎不是获取所单击的MovieClip 名称的方法。

getChildByName() 是这样做的方法吗?如果是这样,有什么想法如何使用 getChildByName() 来获取单击的 MovieClip 的名称吗?

There are multiple MovieClips that will be dynamically placed on stage. These MovieClips are coded to be buttons. I'm trying to figure out--when a user clicks on the MovieClip...figure out which object on the flash stage the user clicked on.

Inside function toggleClick I put the trace statement:

trace("movieClip Instance Name = " + e.target.name);

In the OUTPUT window:

movieClip Instance Name = instance5 
movieClip Instance Name = instance12 
movieClip Instance Name = instance5 
movieClip Instance Name = instance32 
movieClip Instance Name = instance5 
movieClip Instance Name = instance59 

That doesn't seem the way to get a name for the MovieClip that was clicked.

Is getChildByName() the way to do it? If so, any ideas how to use getChildByName() to get the name of the MovieClip that was clicked?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫弦 2024-09-21 10:36:20

在将按钮添加到舞台之前,您可以实际命名它

  var myButton:MovieClip = new MovieClip();
  myButton.name = 'button1';

,或者

  var myButton:MovieClip = new MyButton(); //if you assigned a class name to your MovieClip
  myButton.name = 'button1';

通过您的示例,您可以执行以下操作:

  var comp:Comp = new Comp();
  var monitor:Monitor = new Monitor();

  addItemButton( comp, "comp" , {x:100, y:200});
  addItemButton( monitor, "monitor" , {x:30 , y:50} );


  private function addItemButton(item:MovieClip , itemName:String , params:Object):void
  {
     item.addEventListener(MouseEvent.CLICK , clickHandler );
     item.name = itemName;

     // of course params is not necessary, just making a point of  
     // how to centralize your concerns
     item.x = params.x;
     items.y = params.y;

     addChild( item);
  } 

  private function clickHandler(event:MouseEvent):void
  {
     trace( "button clicked:" + event.currentTarget.name );
  }

Before adding a button to the stage you can actually name it

  var myButton:MovieClip = new MovieClip();
  myButton.name = 'button1';

or

  var myButton:MovieClip = new MyButton(); //if you assigned a class name to your MovieClip
  myButton.name = 'button1';

With your example you could do something like this:

  var comp:Comp = new Comp();
  var monitor:Monitor = new Monitor();

  addItemButton( comp, "comp" , {x:100, y:200});
  addItemButton( monitor, "monitor" , {x:30 , y:50} );


  private function addItemButton(item:MovieClip , itemName:String , params:Object):void
  {
     item.addEventListener(MouseEvent.CLICK , clickHandler );
     item.name = itemName;

     // of course params is not necessary, just making a point of  
     // how to centralize your concerns
     item.x = params.x;
     items.y = params.y;

     addChild( item);
  } 

  private function clickHandler(event:MouseEvent):void
  {
     trace( "button clicked:" + event.currentTarget.name );
  }
愿与i 2024-09-21 10:36:20

在 AS3 中,当您动态创建 MovieClip 时,Flash 会为其分配一个只读实例名称,如您所见(例如,instance12)。查找哪个影片剪辑被单击的最佳方法是简单地使用 MouseEvent 的 currentTarget/target (请在此处查看两者之间的区别:http://www.wastedpotial.com/?p=10)。

你会像这样使用它:

var foo:MovieClip = new MovieClip();
foo.graphics.drawRect(0, 0, 100, 50);
stage.addChild(foo);
foo.addEventListener(MouseEvent.CLICK, clickHandler);

var bar:MovieClip = new MovieClip();
bar.graphics.drawRect(0, 0, 100, 50); bar.y = 100;
stage.addChild(bar);
bar.addEventListener(MouseEvent.CLICK, clickHandler);

//this function will set the x to 100 and the width to 50 of the clicked MovieClip
function clickHandler(e:MouseEvent):void
{
    e.currentTarget.x = 100;
    e.currentTarget.width = 50;
}

In AS3 when you create a MovieClip dynamically flash asigns it a read-only instance name like you have seen (instance12 for example). The best way to find which movieclip was clicked on is to simply use the currentTarget/target of the MouseEvent (see the difference between the two here: http://www.wastedpotential.com/?p=10).

You would use it like so:

var foo:MovieClip = new MovieClip();
foo.graphics.drawRect(0, 0, 100, 50);
stage.addChild(foo);
foo.addEventListener(MouseEvent.CLICK, clickHandler);

var bar:MovieClip = new MovieClip();
bar.graphics.drawRect(0, 0, 100, 50); bar.y = 100;
stage.addChild(bar);
bar.addEventListener(MouseEvent.CLICK, clickHandler);

//this function will set the x to 100 and the width to 50 of the clicked MovieClip
function clickHandler(e:MouseEvent):void
{
    e.currentTarget.x = 100;
    e.currentTarget.width = 50;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文