简体   繁体   中英

How to move any object to a particular position with cursor (Mouse) in game maker studio

I want to move object by cursor to position of any other Object.

In this code apple object is moving with a cursor but it does not stop at (Eight-Object). In other case if i put the Apple object at any other place except Eight-Object, it must go back the start position.

//in step event

xtarget = obj_eight.x;
ytarget = obj_eight.y;

if(x = xtarget and y = ytarget){
  obj_applelemon.x = xtarget;
  obj_applelemon.y = ytarget;     
}
else{
  x = xstart;
  y = ystart;

   if (mouse_check_button(mb_left)){[enter image description here][1]
   x = mouse_x;
   y = mouse_y;
} 
}

Im trying to understand what your goal is. If I read it correctly, You are trying to move an item to another item using the mouse, and if they drop it in a spot that is NOT the destination, it should return to its original position. If that is correct, the code to do that would be similar to below:

**Create**:
startX = 0;
startY = 0;
destinationX = 0;
destinationY = 0;
isGrabbed = false;

**mouse Pressed**:
isGrabbed = true;

**mouse Released**:
isGrabbed = false;
//after user releases, check if collision or not
if(place_meeting(x, y, obj_eight)){
    obj_applelemon.x = destinationX;
    obj_applelemon.y = destinationY;     
}
else{
    x = startX;
    y = startY;
}

**Step**:
destinationX = obj_eight.x;
destinationY = obj_eight.y;

//if object is grabbed, move to mouse location
if (isGrabbed){
    x = mouse_x;
    y = mouse_y;
}

//creation of object code
var object = instance_create(x, y, obj_applelemon);
object.startX = x;
object.startY = y;

Running through the code: In the create, we set the initial start position, the target position variables, and a mouse click variable. if the object is being click on, it will remain true and move with the mouse until the mouse is released. After the mouse is released, it will check to see if the apple collides with obj_eight. If you try to check if they land DIRECTLY on the x and y of obj_eight, it will make it EXTREMELY hard to land exactly on that spot.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM