Question about IEventDispatcher [closed]
What is the logic behind IEventDispatcher?
This is the code I've seen:
var elements : Array = new Array();
var elements2 : Array = new Array();
for (var i:int = 1; i <= 5; i++) {
elements[i] = this['obj' + i];
elements2[i] = this['tracking' + i];
}
for each(var element_1 : IEventDispatcher in elements){
element_1.addEventListener(MouseEvent.MOUSE_OVER, moveUp);
}
for each(var element_2 : IEventDispatcher in elements2){
element_2.addEventListener(MouseEvent.MOUSE_OUT, moveDown);
}
function moveUp(e开发者_JAVA百科:MouseEvent):void{
e.currentTarget.y -= 30;
}
function moveDown(e:MouseEvent):void{
elements[elements2.indexOf(e.currentTarget)].y += 30;
}
I'm not sure what you're asking.. The logic behind using IEventDispatcher there is that a for each() loop it typed and IEventDispatcher is the type applied to the loop. This basically means that everything in the Array or Vector is either an IEventDispatcher or inheriting from IEventDispatcher.
Here's a for each example using the MovieClip type:
var mcs:Array = [new MovieClip(), new MovieClip()];
var i:MovieClip;
for each(i in mcs)
{
trace(i);
}
A for each is much faster than a standard for loop when manipulating elements in an Array or Vector because it doesn't have to spend as much time working out what the object actually is.
The only other thing you need to know is that a TypeError will be thrown if anything in the array being looped through isn't a MovieClip.
加载中,请稍侯......
精彩评论