How do I restrict a drag and drop area?

Get help using Construct 2

Post » Sat May 31, 2014 10:13 am

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!
B
16
S
7
G
1
Posts: 155
Reputation: 3,119

Post » Sat May 31, 2014 3:40 pm

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 :)
When in doubt..or you are lost in a Zombie apocalypse or someone is giving you a hard time..Remember one thing..... Read the Construct 2 Manual..it will save your life.:)
B
35
S
13
G
8
Posts: 318
Reputation: 7,685

Post » Sat May 31, 2014 5:17 pm

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:

Image

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. :)
B
16
S
7
G
1
Posts: 155
Reputation: 3,119

Post » Sat May 31, 2014 8:06 pm

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.
B
17
S
4
G
1
Posts: 332
Reputation: 3,099

Post » Sun Jun 01, 2014 6:02 am

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..
When in doubt..or you are lost in a Zombie apocalypse or someone is giving you a hard time..Remember one thing..... Read the Construct 2 Manual..it will save your life.:)
B
35
S
13
G
8
Posts: 318
Reputation: 7,685


Return to How do I....?

Who is online

Users browsing this forum: alextro, stevenode, tarek2 and 9 guests