Posted By

curiousmedia on 10/15/09


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

benwasilewski
TrentSkunk


What am I REALLY clicking on!?


 / Published in: ActionScript 3
 

Will trace the name of the display object that is actually receiving mouse events. Useful when the target of your mouse event is not what you are expecting. Oh, the fix 80% of the time is mouseChildren = false;

  1. addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void{trace(e.target+" : "+e.target.name);});

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: curiousmedia on October 16, 2009

For when you really get confused, here's how to apply that to every clickable object in your movie makeEverythingIDselfOnClick(this);

function IDselfOnClick(e:MouseEvent):void { trace("you clicked on ["+e.currentTarget+"] "+e.currentTarget.name); }

function makeEverythingIDselfOnClick(target:DisplayObjectContainer):void { trace("target = " + target.name) for(var i:int=0; i

Posted By: curiousmedia on October 16, 2009

aw, c'mon - no code in comments? seriously?

Posted By: curiousmedia on October 16, 2009
makeEverythingIDselfOnClick(this);

function IDselfOnClick(e:MouseEvent):void
{
    trace("you clicked on ["+e.currentTarget+"] "+e.currentTarget.name);
}
        
function makeEverythingIDselfOnClick(target:DisplayObjectContainer):void
{
    trace("target = " + target.name)
    for(var i:int=0; i<target.numChildren; i++)
    {
        var child = target.getChildAt(i);
        if( (child is InteractiveObject) )
        {
            InteractiveObject(child).addEventListener(MouseEvent.CLICK,IDselfOnClick, false, 0, true);
        }
        if( (child is DisplayObjectContainer)&&(child != target) )
        {
            makeEverythingIDselfOnClick(child);
        }
    }
}   
Posted By: curiousmedia on October 16, 2009

there we go!

Posted By: knalle on October 19, 2009

also keep in mind that if you have a MovieClip (or similar) and you want all InteractiveObject children accepting clicks, you could use one single listener on the container object and use "target" to catch the children. But beware of the DisplayObjects that aren't InteractiveObjects (like Shapes) - they will not be refered to when clicking, thus "target" will refer to the container InteractiveObject - unless the "useCapture" in the listener is set to "true" - then it will not respond on clicks on non-InteractiveObjects.

obj.addEventListener(MouseEvent.CLICK, objClick, false);

function objClick(e:MouseEvent):void { trace("ct:", e.currentTarget.name); // traces: obj's name trace(e.target.name); //traces: nested objects name }

Posted By: knalle on October 19, 2009

(oh, i guess I'm just elaborating on thing already posted)

You need to login to post a comment.