AS3 checking if click was on stage or an a movieclip

actionscript-3stagetarget

How can i check if a click was on the stage (not on a other object in front of the stage – just on a place where no other object is) or on a movieclip (name "mcSlideHolder")?

function onMouse_Down(e:MouseEvent):void
{
    trace("MouseDown occured");
    trace("MouseDown target: " + e.target.name);
    trace("MouseDown target.tostring: " + e.target.toString);
    trace("MouseDown currentTarget: " + e.currentTarget);
    if(e.target.name == null || e.target.name == "mcSlideHolder")
    {   
        this._userInput();
    }
}

My Problem is, that e.target.name is null, when clicking on the stage.
When clicking on the mcSlideHolder e.target.name is "instance235".
How would you do this?

Thanks for any advice!

Best Answer

Normally, when you want to detect a click on a specific MovieClip, that is the one you add the listener to:

mcSlideHolder.addEventListener(MouseEvent.MOUSE_DOWN, onSlideHolderMouseDown);

rather than

stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouse_Down);

It sounds like you're doing the latter; adding a listener to the stage, and trying to figure out in the listener function what was clicked on. There is nothing wrong with this, and it can even be efficient if there are many possible things to click on. But in this case, the currentTarget is always going to be the stage, and the target is always going to be the thing you clicked on that generated the event. If that is indeed how you have things set up, you will get a different result--possibly the one you expected--if you do this:

mcSlideHolder.mouseChildren = false;

I'm assuming mcSlideHolder is the instance name. Setting mouseChildren to false will mean that mcSlideHolder will generate the MOUSE_DOWN event rather than instance235 that is its child.

Related Topic