0

I need help with GameMaker Studio 2 v2.3.2.556 project.

My clicking & dragging mechanism has two objects: an object called obj_iron that is being dragged, and an object called obj_cursor that is invisible and always follows the mouse. When I use the cursor to drag the iron, nothing happens. How do I make it so I can drag the iron using the cursor?

Code in obj_cursor in the Step Event

//Making the cursor move toward the mouse
global.picked_up_iron = 0
x = mouse_x
y = mouse_y

//Making the cursor small
image_xscale = 0.1
image_yscale = 0.1

//Checking if the cursor collided with obj_iron
if place_meeting(x,y,obj_iron) 
{
    //Checking if the mouse is held and the cursor is not collided with another obj_iron
    if mouse_check_button(mb_left) and (global.picked_up_iron == 0 or global.picked_up_iron == other)
    {
        //Moving the dragged piece of obj_ironto the obj_cursor's location
        var picked_up_iron = other
        picked_up_iron.x = x - picked_up_iron.sprite_width / 2
        picked_up_iron.y = y - picked_up_iron.sprite_height / 2

        //Telling every the object what the dragged piece of obj_iron is 
        global.picked_up_iron = picked_up_iron
    
    }
} 
else if !mouse_check_button(mb_left)
{
    //Resetting the value of the current piece of obj_iron 
    global.picked_up_iron = 0
}
Stefano Sansone
  • 2,377
  • 7
  • 20
  • 39
  • There can be various things going on when 'nothing happens', so I would recommend setting a breakpoint and debugging your code so you can look into the code step-by-step. My first thought would be that `place_meeting()` does not know which kind of iron is selected, if there are multiple irons in the room. Maybe you can try out `var iron = instance_place(x, y, obj_iron)`, so the `iron` returns the object the cursor is colliding with. – Steven Nov 15 '21 at 07:54
  • Thank you! The code you inserted worked. I really appreciate your help! – ProfoundMaker Nov 15 '21 at 21:55
  • Glad to hear it worked out for you, I'll mark my comment as an answer then. – Steven Nov 16 '21 at 06:28

1 Answers1

0

If there are multiple of the same object in the same room, then place_meeting() does not know which kind of object is selected.

Maybe you can try out Instance_Place

var iron = instance_place(x, y, obj_iron);

This way, the iron returns the object the cursor is currently colliding with.

Steven
  • 1,996
  • 3
  • 22
  • 33