我有一个MovieClip
,里面有一个名为t_bt
的按钮。我已将MovieClip
导出到操作脚本,并为其指定了e_panel
的类名。我使用此代码创建了50个e_panel
实例:
var e_p_y:Number=0;
for ( var i:Number=1;i<=50;i++)
{
var e_p:MovieClip = new e_panel();
e_p.x = 50;
e_p.y = e_p_y;
e_p.t_bt.addEventListener(MouseEvent.MOUSE_UP, f1);
addChild(e_p);
e_p_y = e_p_y+105;
}
现在我想要确定用户在功能f1中按下了哪个按钮。
function f1(event:MouseEvent):void
{
//...what should I write here?
}
答案 0 :(得分:2)
每个事件都有一个名为currentTarget
的属性,它将引用您添加事件侦听器的对象。
所以在你的情况下:
function f1(event:MouseEvent):void
{
var t_bt:DisplayObject = event.currentTarget as DisplayObject; //would be the t_bt instance that was clicked (typed as a basic object)
var e_p:MovieClip = DisplayObject(event.currentTarget).parent as MovieClip //would be the e_p of the item clicked
//so if you wanted to do something like make the whole panel half transparent once clicked
e_p.alpha = .5;
//if you wanted to get the index of the button clicked
trace("Button Clicked: ", e_p.parent.getChildIndex(e_p));
}
相反,事件的target
属性是被点击的实际displayObject(可能与currentTarget
或currentTarget
的子项相同)
答案 1 :(得分:0)
e_panel剪辑必须具有扩展MovieClip的关联E_Panel类,并且(例如)添加索引属性以保存项目的索引。
然后,在f1你可以这样做:
function f1(event:MouseEvent):void
{
var e_panel:E_Panel = event.currentTarget.parent as E_Panel;
trace(e_panel.index);
}
请记住在创建影片剪辑时设置索引属性
希望有所帮助
额外信息
为E_Panel创建一个类,并使用库属性
将其链接到movieClippublic class E_Panel extends MovieClip
{
private var _index:int;
public function E_Panel(index:int)
{
super();
_index = index;
}
public function get index():int {
return _index;
}
}
然后在你的代码中:
var e_p:E_Panel = new e_panel(i); //"i" is the iteration counter
e_p.x = 50;
... //Etc
答案 2 :(得分:-1)
试试此代码::
var e_p_y:Number=0;
for ( var i:Number=1;i<=50;i++)
{
var e_p:MovieClip= new e_panel();
e_p.x=50;
e_p.y=e_p_y;
e_p.t_bt.addEventListener(MouseEvent.MOUSE_UP, f1);
addChild(e_p);
e_p_y=e_p_y+105;
e_p.name= i+'';
}
现在在f1函数::
中编写此代码function f1(event:MouseEvent):void
{
trace(event.currentTarget.name);
}