3
\$\begingroup\$

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?

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

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 :

import flash.events.Event;

//CLICK AND DRAG ( DON'T DO A MOUSE UP )
stage.addEventListener(MouseEvent.MOUSE_MOVE,mv)

function mv(e:Event )
{
    trace(mouseX,mouseY);
}
\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$
    – Rudey
    Commented Jan 25, 2013 at 10:41
  • \$\begingroup\$ 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! \$\endgroup\$
    – Rudey
    Commented Jan 25, 2013 at 15:03
  • \$\begingroup\$ ya. updated the answer with the test-code. \$\endgroup\$
    – Vishwas
    Commented Jan 25, 2013 at 18:57
  • \$\begingroup\$ That's just brilliant. Not what I initially hoped for, but this will fix my problem. \$\endgroup\$
    – Rudey
    Commented Jan 26, 2013 at 21:33
  • \$\begingroup\$ Cool, I didn't know that. Now you just have to force the user to hold that mouse... (no clicking I guess) \$\endgroup\$
    – bummzack
    Commented Jan 26, 2013 at 22:23
3
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ 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. \$\endgroup\$
    – Rudey
    Commented Jan 24, 2013 at 8:27
  • \$\begingroup\$ @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). \$\endgroup\$
    – bummzack
    Commented Jan 24, 2013 at 8:42
  • \$\begingroup\$ 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. \$\endgroup\$
    – Rudey
    Commented Jan 24, 2013 at 9:23
  • \$\begingroup\$ @RuudLenders I see. This is indeed an undesirable situation. Unless you can convince your users to play your game fullscreen ;) \$\endgroup\$
    – bummzack
    Commented Jan 24, 2013 at 10:09
  • \$\begingroup\$ 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 \$\endgroup\$
    – Rudey
    Commented Jan 24, 2013 at 10:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .