Copy link to clipboard
Copied
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));
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.
Copy link to clipboard
Copied
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 Object | event.target | event.currentTarget |
|---|---|---|
| Label | Label | Label |
| Button | Label | Button |
| Form | Label | Form |
(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).
Copy link to clipboard
Copied
Thanks I think I got it now
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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);
}
///////////////////////////////////////////////////////////////
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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
Sprite
DisplayObjectContainer
InteractiveObject
DisplayObject
EventDispatcher
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).
Copy link to clipboard
Copied
I see, thanks for the detail explanation, it really explain all my doubts. And 1 last question, which mean the reference object from library should always assign as movieclip so the compiler will only treated as moviclip? If the the movieclip instance is from stage and not from library, then the there is not need to assign as movieclip ?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
oh I just realise the circle is also a resource from library, thanks for the help lol~:D
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Yes, I'm slowly figuring out that addchild can only accept displayChildObject, thanks for the extra info to clarify my doubts. And the circle is the cause of the error...
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more