Skip to main content
Inspiring
October 26, 2011
Answered

event.target confusion?

  • October 26, 2011
  • 2 replies
  • 4796 views

I'm trying to reusing function by using event.target, but after searching and trying different method online, I found the hardest part to understand is event.target part...

can anyone please explain to me what is the difference between(the example on right showing where I encounter those code):

1.event.target

2.event.target.name

3.event.currentTarget                             //e.g. event.currentTarget.addChild(myMc);

4.event.currentTarget.name                    //e.g. var newswf:URLRequest= new URLRequest( event.target.name +".swf");

5. MovieClip(event.currentTarget)            //e.g. addChild(MovieClip(event.currentTarget));

This topic has been closed for replies.
Correct answer Ned Murphy

Which mean the main culprit for this is because of the circle? because the circle is an object and doesnt recognise it as DisplayObject or movieclip? That means the error would not occur if I'm refering a movieclip instance on stage or from library using as linkage? 


I consider the main culprit to be the compiler and whoever decided to dumb it up.  If you try what you suggest and manually place circles on the stage you will probably see the same error come up....  even if the circle is a resource in the library that is defined as a MovieClip symbol that the compiler should be able to refer to.

2 replies

Ned Murphy
Legend
October 26, 2011

The event target is the object that triggered the event which might or might not be the object that had the event listener assigned to it.  It could be something contined within the object that has the listener assigned to it. 

The event currentTarget is always the object with the listener assigned to it.

Here's a link to an explanation regarding target vs currentTarget:

http://www.wastedpotential.com/?p=10

The name property of any object is a String property.  It is not necessarily the instance name of an object since instance names are manually assigned to objects that are placed on the stage.

MovieClip(event(currentTarget) is casting the object as a movieclip which is sometimes necessary when trying to extract properties or execute methods of objects that are otherwise unknown types to the compiler.


Inspiring
October 26, 2011

Thanks for the explanation, but i still have doubt with MovieClip(event.currentTarget)..

I am making 3circle which will move to front when i try to click them, below are my code:

I get an error using  addChild(event.currentTarget); instead of  addChild(MovieClip(event.currentTarget));....why is that? I thought if I use addChild(event.currentTarget); would mean the same as addChild(circleX);  (depends on which circle I click on)....

/////////////////////////////////////////////////////////

var circle1:Circle = new Circle();

var circle2:Circle = new Circle();

var circle3:Circle = new Circle();

circle1.x = 250;

circle1.y = 175;

circle2.x = 300;

circle2.y = 175;

circle3.x = 275;

circle3.y = 225;

addChild(circle1);

addChild(circle2);

addChild(circle3);

circle1.addEventListener(MouseEvent.CLICK, clickCircle);

circle2.addEventListener(MouseEvent.CLICK, clickCircle);

circle3.addEventListener(MouseEvent.CLICK, clickCircle);

function clickCircle(event:MouseEvent) {

     addChild(MovieClip(event.currentTarget));

   //addChild(event.currentTarget);

}

///////////////////////////////////////////////////////////////

Ned Murphy
Legend
October 26, 2011

If you look at the error is is telling you that the event.currentTarget is only seen as an Object by the compiler, and since there are different types of objects, the compiler won't accept you implying that the object is a DisplayObject, you need to be explicit and tell the compiler that it is a DisplayObject, of which a Movieclip is.  You could also use DisplayObject instead of MovieClip.

While some will defend this behavior on the compiler's part, I consider this to just be annoyance since the compiler should be able to discern that the object is indeed a Moviecli/DisplayObject from a variety of sources it probably has at its disposal. I have no idea why they designed it this way.

Participating Frequently
October 26, 2011

Read this: http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7e55.html

Many user-interface events, like mouse clicks, "bubble," which means that many display objects have a chance to respond to the event.

Imagine that you have a form object that contains a button, and that button contains a label. If the user clicks on the label, the label display object becomes the event target. The click event then "bubbles" up to the parent objects. The label can handle the event, then the button can handle the event, then the form can handle the event, and so on, up to the stage object. The event.target remains the same (the label), the event currentTarget changes each time the event bubbles to the next level and is the current display object. (If the user "missed" the label and still clicked on the button, then the button object would be the event target and the label object would never "see" the event.)

So, if you added an event listener to each of the three objects, then the following table shows the event object properties as it bubbles up the display list:

Processing Objectevent.targetevent.currentTarget
LabelLabelLabel
ButtonLabelButton
FormLabelForm

(currentTarget is a misleading API name, in my opinion.)

And for the rest of your question, event.target.name is the name of target object (i.e Label.name); event.currentTarget.name is the name of the current target object (which could be Label.name, Button.name, or Form.name in my example).

Inspiring
October 26, 2011

Thanks I think I got it now