Actionscript 3 为actionscript中的所有数组元素创建一个addEventListener
我有一系列电影剪辑,代表用户可以单击的按钮,因此我需要使用Actionscript 3 为actionscript中的所有数组元素创建一个addEventListener,actionscript-3,actionscript,Actionscript 3,Actionscript,我有一系列电影剪辑,代表用户可以单击的按钮,因此我需要使用addEventListener功能,以便可以处理单击 我可以使用循环并为每个元素创建一个addEventListener,数组中有26个元素,但我想尝试另一种解决方案,只使用一个addEventListener,并将其应用于数组而不是元素 我想知道如何识别单击了哪个按钮,我的意思是它在数组中的索引是什么 谢谢。将其添加到电影剪辑中。向数组中添加事件侦听器没有多大意义。您基本上是在说“嘿,数组,当您的某些内容发生变化时,请告诉我”,而数组
addEventListener
功能,以便可以处理单击
我可以使用循环并为每个元素创建一个addEventListener
,数组中有26个元素,但我想尝试另一种解决方案,只使用一个addEventListener
,并将其应用于数组而不是元素
我想知道如何识别单击了哪个按钮,我的意思是它在数组中的索引是什么
谢谢。将其添加到电影剪辑中。向数组中添加事件侦听器没有多大意义。您基本上是在说“嘿,数组,当您的某些内容发生变化时,请告诉我”,而数组不是EventDispatcher的子类,所以这是不可能的。但在任何情况下,你都不想知道数组,你想知道movieclip,所以逻辑上要做的事情是创建循环并将其添加到movieclip。你不能将事件侦听器分配给数组 我认为您正在对数组中的每个片段应用不同的事件侦听器函数 对于每个剪辑,可以添加相同的事件侦听器:
clips[i].addEventListener(MouseEvent.CLICK,handleClick)代码>
而handleClick
函数看起来像:
function handleClick(e:MouseEvent):void {
trace(clips.indexOf(e.target)) // outputs index the movieclip that was clicked on
}
你不能直接跳出循环——某些循环必须在某处应用,但你可以间接跳出循环——你可以让VM为你循环。你应该看看
一个简单的应用程序可能是:
// assuming myArr is your array.
myArr.forEach( function(item:*, index:int, array:Array):void
{
item.addEventListener( MouseEvent.CLICK, myClickHandler );
} );
要获取项目的索引,您可能需要做一些更复杂的事情:
myArr.forEach( function(item:*, index:int, array:Array):void
{
item.addEventListener( MouseEvent.CLICK, function( event:Event ):void
{
trace( "my index is", index );
} );
} );
我建议您只需将数组缓存到侦听器函数可以访问的某个位置,然后与一起使用,但如果您认为这一点不可接受,可以通过以下方式使用forEach:
myArr.forEach( function(item:*, index:int, array:Array):void
{
item.addEventListener( MouseEvent.CLICK, function( event:Event ):void
{
trace( "my index is", array.indexOf( item ) );
} );
} );
根据您调用这些方法的频率,简单地使用以下方法可能不会更快:
// probably not best to have this public
protected var myArr:Array = [/* whatever goes in here */]
public function register():void
{
myArr.forEach( addHandler );
}
protected function addHandler( item:IEventListener, index:int, arr:Array ):void
{
item.addEventListener( MouseEvent.CLICK, myClickHandler );
}
protected function myClickHandler( event:MouseEvent ):void
{
trace( event.currentTarget, "is at", myArr.indexOf( event.currentTarget ) );
}
但是,如果不进一步了解您的特定用例,我无法确定。为您的按钮创建一个类,并将事件侦听器添加到该类中。这样,您甚至不必在电影剪辑中循环,因为该方法将是类的一部分您可以为IEventDispatcher
对象创建自己的自定义向量类,该对象具有一个自定义方法,可将事件侦听器添加到其所有元素中。最好的方法是创建一个代理类,作为向量的包装器。
对象。我创建了一个示例来演示这一点:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.IEventDispatcher;
public class Main extends Sprite
{
private var _eventDispatcherVector:EventDispatcherVector;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var redCustomButton:CustomButton = new CustomButton("RED", 0xFF0000);
addChild(redCustomButton);
var blueCustomButton:CustomButton = new CustomButton("BLUE", 0x00FF00);
blueCustomButton.x = 100;
addChild(blueCustomButton);
var greenCustomButton:CustomButton = new CustomButton("GREEN", 0x0000FF);
greenCustomButton.x = 200;
addChild(greenCustomButton);
_eventDispatcherVector = new EventDispatcherVector(Vector.<IEventDispatcher>([redCustomButton,
blueCustomButton,
greenCustomButton]));
_eventDispatcherVector.addEventListener(MouseEvent.CLICK, onCustomButtonClick);
}// end function
private function onCustomButtonClick(e:Event):void
{
var customButton:CustomButton = e.target as CustomButton;
trace("You clicked: " + customButton.name + "\n" +
"Its index is: " + _eventDispatcherVector.indexOf(customButton));
}// end function
}// end class
}// end package
import flash.utils.Proxy;
import flash.utils.flash_proxy;
import flash.events.IEventDispatcher;
use namespace flash_proxy;
dynamic internal class EventDispatcherVector extends Proxy
{
private var _eventDispatcherVector:Vector.<IEventDispatcher>;
public function EventDispatcherVector(eventDispatcherVector:Vector.<IEventDispatcher>)
{
_eventDispatcherVector = eventDispatcherVector;
}// end function
override flash_proxy function getProperty(name:*):*
{
return _eventDispatcherVector[name];
}
override flash_proxy function setProperty(name:*, value:*):void
{
_eventDispatcherVector[name] = value;
}// end function
public function indexOf(searchElement:*, fromIndex:*= 0):int
{
return _eventDispatcherVector.indexOf(searchElement, fromIndex);
}// end function
public function addEventListener(type:String,
listener:Function,
useCapture:Boolean = false,
priority:int = 0,
useWeakReference:Boolean = false):void
{
for each(var eventDispatcher:IEventDispatcher in _eventDispatcherVector)
{
eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
}// end for each
}// end function
}// end class
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
internal class CustomButton extends Sprite
{
public function CustomButton(name:String, color:uint)
{
graphics.beginFill(color);
graphics.drawRect(0, 0, 100, 100);
graphics.endFill();
var textField:TextField = new TextField();
textField.autoSize = TextFieldAutoSize.LEFT;
textField.text = name;
textField.mouseEnabled = false;
textField.x = (100 / 2) - (textField.width / 2);
textField.y = (100 / 2) - (textField.height / 2);
addChild(textField);
this.name = name;
}// end function
}// end class
包
{
导入flash.display.Sprite;
导入flash.events.Event;
导入flash.events.MouseEvent;
导入flash.events.IEventDispatcher;
公共类Main扩展了Sprite
{
私有var_eventdispatchervator:eventdispatchervator;
公共函数Main():void
{
if(stage)init();
else addEventListener(Event.ADDED_TO_STAGE,init);
}//端函数
私有函数init(e:Event=null):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);
var redCustomButton:CustomButton=新的CustomButton(“红色”,0xFF0000);
addChild(红色自定义按钮);
var blueCustomButton:CustomButton=新的CustomButton(“蓝色”,0x00FF00);
blueCustomButton.x=100;
addChild(蓝色自定义按钮);
var greenCustomButton:CustomButton=新的CustomButton(“绿色”,0x0000FF);
绿色自定义按钮。x=200;
addChild(绿色自定义按钮);
_eventDispatcherVector=新的eventDispatcherVector(向量)。([redCustomButton,
蓝色按钮,
绿色按钮),;
_eventDispatcherVector.addEventListener(MouseEvent.CLICK,onCustomButtonClick);
}//端函数
自定义按钮上的私有函数单击(e:事件):无效
{
var customButton:customButton=e.目标为customButton;
跟踪(“您单击了:“+customButton.name+”\n”+
它的索引是:“+_eventDispatcherVector.indexOf(customButton));
}//端函数
}//末级
}//端包
导入flash.utils.Proxy;
导入flash.utils.flash\u代理;
导入flash.events.IEventDispatcher;
使用名称空间flash_代理;
动态内部类EventDispatchevector扩展代理
{
私有var_eventDispatcherVector:Vector。;
公共函数eventDispatchevector(eventDispatchevector:Vector.)
{
_EventDispatchevector=EventDispatchevector;
}//端函数
重写flash_代理函数getProperty(名称:*):*
{
return_eventDispatcherVector[name];
}
重写flash_代理函数setProperty(名称:*,值:*):无效
{
_eventDispatcherVector[名称]=值;
}//端函数
公共函数indexOf(searchElement:*,fromIndex:*=0):int
{
返回_eventDispatcherVector.indexOf(searchElement,fromIndex);
}//端函数
公共函数addEventListener(类型:String,
监听器:函数,
useCapture:Boolean=false,
优先级:int=0,
useWeakReference:Boolean=false):无效
{
对于每个(var eventDispatcher:IEventDispatcher in_eventDispatcher)
{
addEventListener(类型、侦听器、useCapture、优先级、useWeakReference);
}//每个都结束
}//端函数
}//末级
导入flash.display.Sprite;
导入flash.text.TextField;
导入flash.text.textfield自动调整大小;
内部类CustomButton扩展了Sprite
{
公用函数自定义按钮(名称:字符串,颜色:uint)
{
图形填充(彩色);
格拉希
buttonContainer.addEventListener(MouseEvent.CLICK, buttonContainerClickHandler);
private function buttonContainerClickHandler(e:MouseEvent):void
{
var targetButton:Sprite = e.target as Sprite;
//do something with targetButton.
}