Login | Register

Drag and drop objects

July 25, 2006, 16:48 by Calle
[loading]
-->
This is how to make objects follow the mouse when it is pressed upon them. First of all we got to initialize two variables in the create event:

press_x = 0;
press_y = 0;


They are later used in mouse press event:

press_x = mouse_x-x;
press_y = mouse_y-y;


As you can see they are used to save the difference between the object position and the mouse position so that the objects border always will stay relative to the mouse. This is the final code that makes the object drag able, put it in step event:

if (mouse_check_button(mb_left)==true) {
if (position_meeting(mouse_x,mouse_y,self)) {
x = mouse_x-press_x;
y = mouse_y-press_y;
}
}


Example:
http://gmtutorials.com/files/dragex.gm6

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