Login | Register

Draggable Objects Method 1

February 13, 2010, 07:55 by Aspen
[loading]
-->
A simple way to drag objects around in a game.

For the object you want to drag...

1. Add a Create Event

canDrag=false;

2. Add a Step Event

if (mouse_check_button(mb_left)==true)
{
if (position_meeting(mouse_x,mouse_y,self))
{
canDrag=true;
}
}

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

if canDrag=true
{
x=mouse_x;
y=mouse_y;
}


Explaination:
If the mouse is over the object and the LMB is pressed, toggle the object to draggable. When the object is draggable, set the object's x and y equal to the mouse's x and y. When the LMB is released, toggle the object to not draggable.

Weakness:
When first clicked on, the object's origin will jump to the mouse point.

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