• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

How to create a drag and drop object?

I

itameio

Guest
I feel stupid for not realizing how to do this after all these years of using Game Maker and understanding more complex stuff. . .I need to create an object that can be dragged and dropped in the room by the mouse.

The only way I can do that is by having the object jump to mouse position after it is clicked and held. But that makes it jump based on the sprite center, and not be dragged by the point the cursor click on. Does that make any sense?

I tried measuring the distance between the position of object and position of cursor by decreasing the smallest of them from the largest and then have the position of the object increased/decreased based on that distance. But I found that's too complicated and may not even make sense. I've seen an example file for such a thing with old Game maker 8.0 which had very simple code, but I failed to find it online at this time.

So how can I make the object dragged while the point clicked on by the mouse as the center? to make it stop jumping?
 

JFitch

Member
Mouse Button Pressed
Code:
xx=x-mouse_x;
yy=y-mouse_y;
Mouse Button Held
Code:
x=mouse_x+xx;
y=mouse_y+yy;
When the mouse is pressed, you store the x and y differences from the mouse. Then, when the mouse is held, you set the position based on the stored differences.
 
I

itameio

Guest
See: Dragging and dropping without jumping

You got the idea about the difference between mouse and instance position right, but what you should have done is to not care about which one's bigger. Just add the difference and the sign of the difference would imply which way to offset.
Mouse Button Pressed
Code:
xx=x-mouse_x;
yy=y-mouse_y;
Mouse Button Held
Code:
x=mouse_x+xx;
y=mouse_y+yy;
When the mouse is pressed, you store the x and y differences from the mouse. Then, when the mouse is held, you set the position based on the stored differences.

Thank you!
 
Top