The target and currentTarget attributes of the Event class in AS3

Source: Internet
Author: User
Original article address:AS3 Medium Event The target and currentTarget attributes of the class. Author:Stupid fish XiaoYu

 During event processing, an instance of the event class is automatically generated and passed to the listener function. With this parameter, you can use the attributes and methods of the event class. The target and currentTarget attributes are very similar.
For simple event processing, it is not necessary to distinguish between target and currentTarget. Because they generally point to the same object. For example, there is a video editing instance named mc on the stage, and the following code registers a click event for this instance.

Mc. addEventListener (MouseEvent. CLICK, this. test );
Function test (e: MouseEvent)
{
  Trace(e.tar get. name, e. currentTarget. name );
}
The output results are the same. Both the target and currentTarget attributes reference the video editing instance on the stage. However, in a relatively complex display list, these two attributes are different.
The following examples show the similarities and differences between the target and currentTarget attributes.
1. Create a Flash document
2. Change the name of Layer 1 to as. Click 1st frames on the layer to open the action panel and enter the code: var sp1: Sprite = new Sprite ();
Var sp2: Sprite = new Sprite ();

This. addChild (sp1 );
Sp1.addChild (sp2 );

DrawRect (sp1, 0xff0000, 200 );
DrawRect (sp2, 0x0000FF, 100 );

// Draw a rectangle
Function drawRect (obj: DisplayObject, c: uint, l: int): void;
{
  Obj. graphics. beginFill (c );
  Obj. graphics. drawRect (0, 0, l, l );
}
The above Code creates the Sprite class instance sp1 in the main timeline, and the Sprite class instance sp2 In the sp1 instance. If you register a click event listener for parent sp1, When you click sp1, both target and currentTarget point to sp1. When you click sp2, target points to sp2, while currentTarget points to sp1. Therefore, in many applications, currentTarget points to the parent level. The Code is as follows: sp1.name = "sp1 ";
Sp2.name = "sp2 ";
Sp1.addEventListener (MouseEvent. CLICK, clickFunc );

Function clickFunc (e: MouseEvent): void
{
  Trace(e.tar get. name, e. currentTarget. name );
  
}
3. Test the video. Click the size rectangle to view the information.
If a listener is registered for both the child and parent levels, the target attribute refers to the clicked target, and the currentTarget attribute refers to the event being processed, that is, the activity target, because 3.0 of event processing involves three phases: capture, target, and bubble, and the bubble mechanism is used by default, When you click sub-level, the currentTarget attribute should first point to the target and bubble up, that is, point to sp2 first, and then to sp1.
Add the sp2 registration event listener for the Program: sp1.addEventListener (MouseEvent. CLICK, clickFunc );
Sp2.addEventListener (MouseEvent. CLICK, clickFunc );
Function clickFunc (e: MouseEvent): void
{
  Trace(e.tar get. name, e. currentTarget. name );
}
Therefore, the currentTarget attribute must have two conditions: first, it registers the listener, and second, it is processing the event, and the target attribute is the target in the event stream. For example, if you click sp2, regardless of how the event is bubbling or whether currentTarget points to anyone, the target points to sp2.
The target attribute is in the target stage of the event stream, while the currentTarget attribute is in the bubble stage, target stage, and capture stage of the event stream. Take the click event as an example. When the event stream is in the target stage, the currentTarget attribute is the same as the target attribute. When the event stream is in the bubble stage and capture stage, the target attribute always points to the clicked object, while the cu r1_target attribute points to the object of the current event activity.
Even if no event stream is processed, the target and currentTarget attributes must be differentiated. For example, mc1 is created on the stage, mc2 is created on mc1, and mc2 is located on the top of mc1.
When processing an event for the parent object mc1, use the following code 1: mc1.addEventListener (MouseEvent. CLICK, fun)
Function fun (e: MouseEvent)
{
Trace(e.tar get. name)
}
When testing a video, click mc1, output mc1, and click mc2, and output mc2. If you want to use the e.tar get attribute to always point to mc1, you can use the mouseChildren attribute of the container object:

Mc1.mouseChildren = false;

When the mouseChildren attribute of mc1 is set to false, the child objects of mc1 cannot process mouse events. At this time, mc1 is output no matter whether mc1 or mc2 is clicked. Code 2: mc1.mouseChildren = false
Mc1.addEventListener (MouseEvent. CLICK, fun)
Function fun (e: MouseEvent)
{
Trace(e.tar get. name)
}
Because the currentTarget attribute points to the object of the current event activity, while mc1 registers the click event, mc2 does not register the click event, that is, the mc1 event is active. Therefore, whether you click mc1 or mc2, The currentTarget attribute must point to mc1.
Code 3; mc1.addEventListener (MouseEvent. CLICK, fun)
Function fun (e: MouseEvent)
{
Trace (e. currentTarget. name)
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.