How do I restrict a drag and drop area?

0 favourites
  • 5 posts
From the Asset Store
Now you can drop everything in your game with sound :)
  • Hi,

    I need a certain object to be draggable only within a certain area, as in an invisible rectangle.

    I'm not sure how to accomplish this. Any help would be appreciated!

  • hey pirx Its a good question,but before we can try to give you a good answer can you be more specific..

    Some extra details would be appreciated ..like...

    What do you want the draggable object to do when it reaches the border of the restricted area?

    Do you want it to drop?

    Or just stop moving?

    Or is the mouse cursor movement restricted to an area as well as the object is being dragged..

    if you can answer some of these questions you might already have the answer you need..

    Each game is different so it would help if we knew exactly what you wanted to do

    otherwise we can only guess to what specific events or actions you would need

    happy to help out if you can elaborate on the matter

  • Hi, good points.

    So the cursor should be free to go anywhere, but once the object is grabbed it can be only dragged within a specified area (which preferably constitutes a sprite). When it reaches the border it just stops, i.e. stays grabbed but can't be moved any further.

    Here's a mockup:

    So in this example, say I want the bomb to be draggable over the red area, so that I can enable physics when it is dropped and have it fall on the island. Since the bomb explodes on-collision with the island I don't want the player to be dragging it around the menu, island etc. but only in the specified area. I hope it's clearer now.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • My advice is:

    Give your object an instance variable called "picked"

    While dragging, set it to 1.

    Then

    Conditions: every tick, object dragging = 1

    Action: object set X= clamp (self.x, leftborder, rightborder)

    Action: object set Y= clamp (self.y, upborder, bottomborder)

    Obviously, use the coordinates you want to limit in leftborder, rightborder etc. I think this should work, not sure though, don't have c2 under my hand atm.

    Clamp function limits a given value between the next two values you give, so I think this will be ok.

  • Yes pirx , Windwalker is right....Once you have picked up your object you can restrict the motion of the Cursor and object to any location you like or even snap to a location by clamping the Mouse or objects coordinates..

    Its probably the best way ...

    another way might be to assign a solid behavior to the object and have a invisible solid border ...and PIn the object to the mouse cursor sprite position with a ROPE style pin...But i dont think this is as reliable as clamping ...

    but it depends on the feel and requirements you are after...

    each game is different and has its own unique mechanics..

    like everything ..just play around to you find what works best for you..

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