Window Bound Mouse

This forum is currently in read-only mode.
From the Asset Store
Get Ready for the New Wave of "Bottom Screen" Games!
  • Hey guys, is there a way to limit mouse movement to be locked within the boundary of the window?

    For instance, I'm using drag and drop on a sprite; if I drag the sprite out of the window and let go of the mouse button, it doesn't recognize that I've let go of the button, which in turn makes the drop event not trigger properly even when the sprite is brought back in. I'd like to avoid all that by just not allowing the mouse to go out of the window. Doable?

  • Yes, by replacing the system cursor with a sprite (set the sprite's hotspot to the upper left corner just like a windows mouse cursor). The MouseKeyboard object has an action called "Set cursor visible", which you set to invisible. Also add one event:

    + System: Always (every tick)

    -> mouseSprite: Set position to Clamp(MouseX, 0, DisplayWidth), Clamp(MouseY, 0, DisplayHeight)

    EDIT: You may also make the range smaller, if it suits your needs. Just change the values for X and Y, eg. Clamp(MouseX, 20, DisplayWidth - 20), Clamp(MouseY, 2, DisplayHeight - 16)

  • Oh SWEET! I'll try that out right now. Thanks for that.

    On a similar topic, is there a way then to make the clamp a non-rectangle sprite too? I'd like to limit the drag area to within certain paths, so something like "while dragging clamp to sprite area" or some such.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Oh boo. Same problem still, the sprite cursor stays within the clamp, but the actual mouse cursor doesn't so problems remain with drag and drop weirdness.

    (I also applied clamps to the draggable objects, but to no avail.)

    Still, thanks for that clamp feature, that's really handy. I suppose I'll just have to make it fullscreen only, though I'd really rather not resort to that.

  • The mouse has a higher priority than an application, so it's possible to move off the window and click before the mouse can be clamped. A solution is to force the mouse to the center of the window and just move a sprite when the mouse moves. That is only needed when dragging, otherwise the mouse should be free to move as normal. I also implemented drag n drop with events since the behavior uses the actual mouse position.

    http://dl.dropbox.com/u/5426011/examples6/Custom%20drag%20drop.cap

  • Holy heck! +1000 internets good sir. That's really awesome, thank you for taking the time to do that up. Awesome I say!

  • Hi R0J0hound, I am not able to open the cap file that you posted. Could you please send the link again.

    Basically I am also looking for some functionality where I lock the user mouse movement to only a smaller size within the game window while using drag drop and I need help with that.

    Thanks,

    Ravindra

  • That is quite good, I wonder how it works with a scrolling game:

    Position cursor X: Clamp(.x+mousex-scrollx,ScrollXLeft, ScrollXRight)

    Position cursor Y: Clamp(.y+mousey-scrolly,ScrollYTopt, ScrollYBottom)

    ????

    I´ve tried that, but you have to keep moving the mouse while scrolling

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)