Getting mouse coordinates on stage

In ActionScript 3.0, displayObject instances have mouseX/mouseY properties; MouseEvent instance have localX/localY properties and stageX/stageY properties.

If there are other displayObject instances on stage and when cursor rolls over any of them, mouseX/mouseY becomes relative to that instance as opposed to relative to stage. To get the mouse coordinates relative to the stage, use stageX/stageY properties.

Or use localToGlobal method of displayObject class. Like this:

import flash.events.MouseEvent;
import flash.text.TextField;
import flash.geom.Point;
//var _foo:TextField;
function onMouseMove(evt:MouseEvent){
//trace(evt.target.mouseY);
var target:* = evt.target;
//_foo.text = target.mouseY;
//_foo.text = evt.localY.toString();

var location:Point = new Point(target.mouseX, target.mouseY);
location = target.localToGlobal(location);
_foo.text = target.mouseY + ": "+ location.y;}

//root.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);

Advertisements

20 Responses to “Getting mouse coordinates on stage”

  1. chihilatte Says:

    er, can’t you use the MouseEvent.stageX and stageY properties?

  2. maohao Says:

    chihilatte, Thanks for the lead!;)

  3. Gaurav Gupta Says:

    stage.addEventListener(MouseEvent.CLICK,func);

    function func(e:MouseEvent):void
    {
    trace(e.target.mouseX);
    trace(e.target.mouseY);
    }

  4. Woad Says:

    Chihilatte :
    You can get the MouseEvent.stageX and stageY properties, sure, but you have to wait until the user uses the mouse (moves it, clicks it, w/e).

  5. George Garchagudashvili Says:

    Hi there, nice tutorial tnx ๐Ÿ˜‰

  6. Jesus Briones Says:

    you can use

    target.stage.mouseX
    target.stage.mouseY

    works like a charm

  7. Jesus Briones Says:

    just modifying Gauravs example alittle bit, so that it doesnt matter if you have any objects on top of the stage when you mouse over.

    stage.addEventListener(MouseEvent.CLICK,func);

    function func(e:MouseEvent):void
    {
    trace(e.target.stage.mouseX);
    trace(e.target.stage.mouseY);
    }

    • Jack Says:

      Hi, how would you put the mouse x, y coordinates into a Dynamic Text field instead of tracing it? sorry if the answer to this is obvious but I’m really new to AS3.

      • Elliott Says:

        Create 2 dynamic text fields on the stage called mouseX_txt and mouseY_txt.
        put on your actions frame.

        done deal!
        by the way i wouldnt of been able to solve this without the previos post thank you all!

        stage.addEventListener(Event.ENTER_FRAME, mouseyPos);
        function mouseyPos (e:Event):void
        {
        var mouseXpos:Number = e.target.stage.mouseX;
        var mouseYpos:Number = e.target.stage.mouseY;
        mouseX_txt.text = String(mouseXpos);
        mouseY_txt.text = String(mouseYpos);
        }

    • matasoy Says:

      you must be a king.
      thanks bro, i really could not get it.
      it taken my 2 hour ๐Ÿ˜€

  8. Kit Says:

    Hello,
    just wanna say thanks for this great blog entry as well as the most helpful comments here.

    ๐Ÿ™‚

  9. Elliott Says:

    Create 2 dynamic text fields on the stage called mouseX_txt and mouseY_txt.
    put on your actions frame.

    done deal! ๐Ÿ˜€
    by the way i wouldnt of been able to solve this without the previos post thank you all!

    stage.addEventListener(Event.ENTER_FRAME, mouseyPos);
    function mouseyPos (e:Event):void
    {
    var mouseXpos:Number = e.target.stage.mouseX;
    var mouseYpos:Number = e.target.stage.mouseY;
    mouseX_txt.text = String(mouseXpos);
    mouseY_txt.text = String(mouseYpos);
    }

  10. kanu Says:

    when to use e.stageX
    and when to use just mouseX

  11. Tucker Connelly Says:

    Great tutorial–just what I needed!

  12. arun Says:

    //create dynamic text “_foo” and try this

    function ifmove(evt:MouseEvent) {
    _foo.text = stage.mouseX + “: “+ stage.mouseY;
    }
    stage.addEventListener(MouseEvent.MOUSE_MOVE, ifmove);

  13. promovare prin facebook Says:

    promovare prin facebook…

    […]Getting mouse coordinates on stage « :maohao:[…]…

  14. DavidsDesign Says:

    Hi, my name is David and I have an AS3 question.

    I’ve got a flying sheep on my stage which is draggable (this works). It hovers above a globe. What I want is when I drag the sheep to the left half of the stage, the globe starts turning clockwise, and when the sheep is on the right half the globe has to start turning counter clockwise. So, if it works it will create the illusion that the sheep is flying and that the user has the power to move the sheep around the globe.

    PLEAAAASSSEEE, I hope someone can help me out! I can’t find anything about this topic on the internet…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: