Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was wondering if it is possible to have my flash game determine the position of the mouse, even when the cursor is not inside the stage.

My movie clip should move horizontally between x = 100 and 500, using mouseX for movement:

private var previousX : int;
private var mc : MovieClip;
private function update(e:Event = null):void
    var deltaX : int = (mouseX - previousX) / 2;
    mc.x = Math.min(Math.max(mc.x + deltaX, 100), 500);
    previousX = mouseX;

The above code works fine, as long as the cursor is above my flash game. When the cursor is outside the stage, mouseX stops updating. I realise that mouseX is probably not what I'm looking for, so I was wondering if there was any other way of finding the cursors position.

I hoped there was a way to set the mouse position directly, so I could center it after every update, so it would almost never be able to leave the frame in the first place. Is that possible?

share|improve this question
up vote 2 down vote accepted

There is only one way to determine it, for which you will need to make the behavior from mouse-move to click-and-drag. When mouse is clicked over the stage, and then moved outside, it's position (x,y) can be determined. Otherwise, i think it's not possible. I had a similar situation, where ultimately, we were forced to make the game full-screen.

Here is how to test it :



function mv(e:Event )
share|improve this answer
Are you telling me that when you click inside the stage, and then drag outside the stage, you can find it's position outside the stage? Going to test that now. – Ruud Lenders Jan 25 '13 at 10:41
Sorry for spamming but I just need to know... is really possible to determine the mouse coordinates, when you drag it out the stage? I can't test it right now... But if it is posslbe, that would be great. No, that would be amazing! – Ruud Lenders Jan 25 '13 at 15:03
ya. updated the answer with the test-code. – Vishwas G Jan 25 '13 at 18:57
That's just brilliant. Not what I initially hoped for, but this will fix my problem. – Ruud Lenders Jan 26 '13 at 21:33
Cool, I didn't know that. Now you just have to force the user to hold that mouse... (no clicking I guess) – bummzack Jan 26 '13 at 22:23

You can't confine the system cursor to a region with Flash. What you can do is hide the system mouse-cursor using Mouse.hide() (only works when the cursor is over your game-area), then use a custom cursor instead. There's also an event called Event.MOUSE_LEAVE which will fire whenever the mouse leaves the stage area. You can't tell where the mouse will be on screen, but at least you know that it's no longer inside your game-area. Eg.

stage.addEventListener(Event.MOUSE_LEAVE, handleMouseLeave);

Here's a tutorial that explains the above in more detail.

share|improve this answer
Damn, not the answer I was hoping for. If it's not possible, I'll have to rethink my games controls... Anyway, thanks for the clear answer. – Ruud Lenders Jan 24 '13 at 8:27
@RuudLenders Well, if you have a custom cursor you can keep it at the position it was the moment before the mouse left the stage and assume this to be the mouse-position for subsequent updates. If you really need to know the mouse-position outside of your game, you could also use JavaScript calls to get mouse-positions from the browser (via ExternalInterface). This will make it harder/impossible to integrate the game into platforms like kongregate though (as you'll require special JavaScript alongside your swf). – bummzack Jan 24 '13 at 8:42
A custom cursor is not my goal. I want an movie clip to move in the same direction that the mouse is moved, similar to how you rotate the camera in FPS games. As you pointed out, using JavaScript for this is a bad idea, since I want my game to be compatible with most game portals. – Ruud Lenders Jan 24 '13 at 9:23
@RuudLenders I see. This is indeed an undesirable situation. Unless you can convince your users to play your game fullscreen ;) – bummzack Jan 24 '13 at 10:09
Even in fullscreen mode, the cursor will eventualy reach the bounds of the screen. If you keep moving your mouse to the left, mouseX will eventually be 0 all the time, making it impossible for me to determine which direction the mouse is moving. That's why I was hoping to be able to center the mouse position after every update, just like FPS games. By the way, no, I'm not making an FPS. :P – Ruud Lenders Jan 24 '13 at 10:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.