Login | Register

Draggable Objects Method 3

February 13, 2010, 08:22 by Aspen
[loading]
-->
A smoother and more accurate method for dragging objects in game.

For the object you want to drag...

1. Add a Create Event

canDrag=false;
diffX=0;
diffY=0;
clicked=false;

2. Add a Step Event

if (mouse_check_button(mb_left)==true)
{
if (position_meeting(mouse_x,mouse_y,self))
{
canDrag=true;
if (clicked=false)
{
diffX=mouse_x-x;
diffY=mouse_y-y;
clicked=true;
}
}
}

if (mouse_check_button(mb_left)==false)
{
canDrag=false;
clicked=false;
}

if canDrag=true
{
x=mouse_x-diffX;
y=mouse_y-diffY;
}


Explaination:
If the mouse is over the object and the LMB is pressed, toggle the object to draggable and remember where the mouse is in relation to the object's origin (offset). When the object is draggable, set the object's x and y equal to the mouse's x and y (minus the offset). When the LMB is released, toggle the object to not draggable.

Stength:
This method only records the offset when the object is initially clicked on. So the point of the object that was clicked on remains with the mouse pointer.


Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games