Actionscript 3: Event.target returns children of movieclip

Problem:

You’ve added a listener to a movieclip, but the target returned is another movieclip or sprite within that movieclip.

Solution:

myMovieclip.mouseChildren = false;

Yeah, that’s all. Now the movieclip you added the listener to will be the only thing returned as the target of an event. If you want a little more proof before you go through all the trouble to add that line of code, then check my example below:

This movie requires Flash Player 9

Here’s the code (on the timeline):

blockLeft.addEventListener(MouseEvent.CLICK, clickBlock);
blockLeft.buttonMode = true;
blockLeft.mouseChildren = true;
 
blockRight.addEventListener(MouseEvent.CLICK, clickBlock);
blockRight.buttonMode = true;
blockRight.mouseChildren = false;
 
function clickBlock(e:Event):void
{
	txtTarget.text = "e.target.name: " + e.target.name;
}

3 Comments »

  1. Wow thank you so much I was having so much trouble with this.

    Comment by Varkson — July 10, 2010 @ 9:22 am

  2. Thanks!

    I’m just getting started with Actionscript in Flash Builder and it’s these little bits that are there to trip me up!

    Thanks again.

    Comment by John R — March 26, 2011 @ 7:40 pm

  3. Thanks much, just spent a few hours trying hair-brained schemes to do as you have done above very easily. Well done!

    Comment by michael ray — November 17, 2011 @ 6:24 pm

Leave a comment