How do I capture the position of a mouseclick from the user in my Flash window using Actionscript 3.0?
A:
Location defined by what context? The whole page? One or more specific clickable controls?
le dorfier
2009-04-02 17:51:54
A:
You may query any DisplayObject's mouseX and mouseY whenever you like.
jedierikb
2009-04-02 17:54:57
+1
A:
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.
}
Brian Hodge
blog.hodgedev.com
Brian Hodge
2009-04-03 01:30:50
Thanks for the good catch, Brian! I agree with your answer, and have removed mine to reduce clutter.To be honest, I also never use inline handlers in my own real-world AS3 code, but only when giving simple examples. I should really practice preaching what I practice. :)
Ron DeVera
2009-04-03 18:44:48
A:
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; }
Bryan Grezeszak
2009-04-11 00:57:07