Skip navigation
Currently Being Moderated

event.target confusion?

Oct 26, 2011 10:45 AM

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));

 
Replies
  • Currently Being Moderated
    Oct 26, 2011 11:22 AM   in reply to vincentccw

    Read this: http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b902 04-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).

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 26, 2011 11:33 AM   in reply to vincentccw

    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.

    
     
    |
    Mark as:
  • Currently Being Moderated
    Oct 26, 2011 12:42 PM   in reply to vincentccw

    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.

     
    |
    Mark as:
  • Peter Celuch
    505 posts
    Nov 17, 2005
    Currently Being Moderated
    Oct 26, 2011 12:44 PM   in reply to vincentccw

    Hi,

     

    event.target and event.currentTarget are of type Object. Method DisplayObjectContainer.addChild() expects an instance of a DisplayObject class, that's why you get a compile-time error.

    Almost anything can dispatch an event, but only DisplayObject can be added as a child of an DisplayObjectContainer.

     

    I your case, you know that the target is MovieClip (the circle), but the compiler doesn't know it so you have to cast it as a MovieClip.

     
    |
    Mark as:
  • Peter Celuch
    505 posts
    Nov 17, 2005
    Currently Being Moderated
    Oct 26, 2011 1:09 PM   in reply to vincentccw

    I believe that Circle is extending MovieClip so it is MovieClip. The circle is many things, you just have to say the compiler which one you mean

    This is the inheritance of MovieClip:

    MovieClip  Inheritance Sprite Inheritance DisplayObjectContainer Inheritance InteractiveObject Inheritance DisplayObject Inheritance EventDispatcher Inheritance Object

     

    The main culprit is the event.currentTarget property, which returns the safest option, Object. But it doesn't mean the circle becames only Object and nothing more, it means that the circle is just treated as just-an-object until you explicitly say "treat it as a MovieClip" by casting it as MovieClip: MovieClip(event.currentTarget).

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 26, 2011 1:13 PM   in reply to vincentccw

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points