开发者

Question about IEventDispatcher [closed]

开发者 https://www.devze.com 2023-03-07 09:56 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号