yannick
yannick

Reputation:

how to capture mouseclick in actionscript 3.0

How do I capture the position of a mouseclick from the user in my Flash window using Actionscript 3.0?

Upvotes: 0

Views: 14324

Answers (4)

Brian Hodge
Brian Hodge

Reputation: 2135

Ron DeVera is close, but I wouldn't use an inline function, and the object passed to the function is not Event, but MouseEvent.

stage.addEventListener(MouseEvent.CLICK, _onStageMouseDown);

function _onStageMouseDown(e:MouseEvent):void
{
    trace(e);
}

//traces
//[MouseEvent type="click" bubbles=true cancelable=false eventPhase=2 localX=96 localY=96 stageX=96 stageY=96 relatedObject=null ctrlKey=false altKey=false shiftKey=false buttonDown=false delta=0]

All of the properties in the output above are available through the object that gets passed to the Event Listener Method, _onStageMouseDown(e:MouseEvent); Hence the following

function _onStageMouseDown(e:MouseEvent):void
{
    trace(e.localX);
    trace(e.stageX);
    //Note that the above two traces are identical as we are listening to the stage for our MouseEvent.
}

Upvotes: 3

Bryan Grezeszak
Bryan Grezeszak

Reputation: 959

They explained it well, but here's the full code to illustrate it a little more for you:

addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler(event: MouseEvent) : void
{
    // these are the x and y relative to the object
    var localMouseX: Number = event.localX;
    var localMouseY: Number = event.localY;

    // these are the x and y relative to the whole stage
    var stageMouseX: Number = event.stageX;
    var stageMouseY: Number = event.stageY;
}

Upvotes: 1

jedierikb
jedierikb

Reputation: 13109

You may query any DisplayObject's mouseX and mouseY whenever you like.

Upvotes: 0

dkretz
dkretz

Reputation: 37655

Location defined by what context? The whole page? One or more specific clickable controls?

Upvotes: 0

Related Questions