0
\$\begingroup\$

I am attempting to create a click and drag system in GameMaker 2, for example a dress-up game or maybe a puzzle. How would I go about getting the objects to click and drag?

I'm trying to get it so I can click and drag a piece of clothing, for example, and drag it onto a character and then drag another piece of clothing on as well.

\$\endgroup\$
  • \$\begingroup\$ What have you tried so far? Try to be specific about your issue, so we can focus on one thing and help you work it out. \$\endgroup\$ – liggiorgio May 22 '18 at 14:42
  • \$\begingroup\$ I've tried having an object snap to my mouse x and y cords when left mouse button is pressed but the problem is everything I set that to snaps to my mouse \$\endgroup\$ – sketcherskt May 27 '18 at 16:51
1
\$\begingroup\$

If all your objects move at the mouse position at the same time, probably their Step event looks like this:

// Click and drag object
if (mouse_check_button(mb_left) && position_meeting(mouse_x, mouse_y, self))
{
    x = mouse_x;
    y = mouse_y;
}

Or at least, this is the GML equivalent of what is happening.

The code above doesn't take into account whether an instance is actually being clicked on by the user, it cares only about the button click itself.

Step 1: Individual instance flag

We give every instance a variable, with a starting value of false:

grabbed = false;

We set its value to true when the mouse button is pressed and conditions are met; and set it to false when the mouse is released. We can do this by splitting the code between Left Pressed event:

// Now instance can move
grabbed = true;

And Global Left Released event:

// Now instance must stop
grabbed = false;

Finally our Step event will loop like this:

// Move instance if selected
if (grabbed)
{
    x = mouse_x;
    y = mouse_y;
}

Now objects are dragged only if mouse click happened inside their collision mask.

Step 2: Global instance flag

The code works fine so far, except if multiple instances are overlapping when you click, they will all snap to the mouse position! To prevent this, we must make sure one instance only is 'grabbed' when clicking before dragging it. To do so, we can use a global variable instead of an individual flag to tell what instance is picked by the cursor, and move it accordingly:

global.grabbed = noone;

This variable will hold the id of the instance that must move around. So, instead of a true/false approach, we have a noone/id variable that specifies which particular instance is being moved around. So:

Left Pressed event:

// Check if instance can be dragged
if (global.grabbed == noone)
    global.grabbed = self;

Global Left Released event:

// Stop dragging
global.grabbed = noone;

Step event:

// Move object if currently grabbed
if (global.grabbed == self)
{
    x = mouse_x;
    y = mouse_y;
}

You can also use the global.grabbed variable to get info about what the player has picked.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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