The
Flash CS3 guide talks about it, but I do think that it does not render the concept properly.
I am talking about the properties target and currentTarget of the classes included in the package flash.events ( meaning all the events classes of
Actionscript 3.0 ).
It seems that using those two above properties to retrieve the object that has dispatched the event, we find substantial differences that cause troubles to the developer.
Let us see what I am on about?
As an example, we are using a listener to intercept a mouse event on a MovieClip.
I will use the events MOUSE_DOWN and MOUSE_UP of the MouseEvent Class to which I associate a function. Once called, this function will execute startDrag and stopDrag of the MovieClip onto which the events happened.
To realise the above, I will use the property target and currentTarget of the MouseEvent Class to retrieve the clicked MovieClip from the function during the event and so, assign the startDrag.
I create a FLA and save it as ?main.fla?, into which I create a MovieClip in library (shaped as you wish) containing a dynamic text field.
I drag two instances of that MovieClip on stage and I name them ?test_0_mc? and ?test_1_mc?.
Now I write the code:
Code:
test_0_mc.addEventListener(MouseEvent.MOUSE_DOWN,trascina);
test_0_mc.addEventListener(MouseEvent.MOUSE_UP,lascia);
test_1_mc.addEventListener(MouseEvent.MOUSE_DOWN,trascina2);
test_1_mc.addEventListener(MouseEvent.MOUSE_UP,lascia2);
function trascina(m:MouseEvent):void
{
m.currentTarget.startDrag();
}
function lascia(m:MouseEvent):void
{
m.currentTarget.stopDrag();
}
function trascina2(m:MouseEvent):void
{
m.target.startDrag();
}
function lascia2(m:MouseEvent):void
{
m.target.stopDrag();
}
If we do try to drag the left MovieClip (test_0_mc), we realise that everything goes smooth and Flash from the command m.currentTarget retrace the clicked MovieClip.
Instead, if we do try to drag the right MovieClip, from the error returned by Flash, it seems that it does not find the property startDrag of the TextField Class.
Conclusion, following the logic:
- if we associate the event to an empty MovieClip, best to use the property target
- if we associate the event to a MovieClip containing different objects, best to use the property currentTarget
See you soon!