Central Games Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Click + Drag

Go down

Click + Drag Empty Click + Drag

Post by Gamerz Mon Feb 09, 2009 9:08 pm

I've seen this asked many times, and have just been faced with the same problem with a level editor, while trying to add a Dock: How do you click and drag only the TOP most instance? I think I've come up with a way, but I'm not completely sure how well it will work.

First we need to know the order that GameMaker processes instances. First, it goes through the Depth, and then by id. What we want, is the depth. The lower depth instances ALWAYS get processed first, in order to get the Depth effect. So, we want the items to be dragged to have the lowest depth, in order for them to be affected by an event first, such as a click.

The problem is that by increasing the depth, we're also going to make that instance be drawn on the bottom. If we want our draggable instance to stay on top, we need to temporarily increase the depth, but ONLY while drawing. The best way to do this is to set the depth to a very low number (let's say, -100) at the beginning of the draw event, and then set it back to it's high value at the end of the event. This way, the order of processing shouldn't be interrupted, and the instance will still be drawn above all other instances.

But what about dragging it? We set the depth low for a reason, so that the instance should be clicked before all other instances get the chance to do so. But, since the click will eventually affect all instances below above it during the rest of the step, all of those other instances will also be dragged. The only way to do this is to set a variable in each instance, and if that variable is true, then allow the instance to be dragged. When we click on the top(bottom)-most instance, we need to set this variable to false in ALL instances except this one. This could be done by using the following code:

Code:
with (all)
{
    if (variable_local_exists(canDrag)) // There are probably instances that don't have the dragging variable // within the room
        canDrag = false;
}
// Then, we need to set our top instance to let it be dragged, since it would've also been set to false
canDrag = true;

After doing this, in the step event for every dragable instance, we could check if the mouse is pressed, and then if 'canDrag' is equal to true. This would single out ONLY the instance that was set to be draggable by the above code.





It doesn't cover EVERY possibility of D&D functionality (such as making the last clicked instance the highest drawn).
Gamerz
Gamerz
Newbie
Newbie

Number of posts : 38
Warning :
Click + Drag Left_bar_bleue0 / 1000 / 100Click + Drag Right_bar_bleue

Reputation :
Click + Drag Left_bar_bleue20 / 10020 / 100Click + Drag Right_bar_bleue

Registration date : 2008-11-05

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum