简体   繁体   English

如何在游戏制作工作室中使用光标(鼠标)将任何对象移动到特定位置

[英]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.在其他情况下,如果我将 Apple 对象放在除八对象之外的任何其他位置,它必须返回起始位置。

//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.运行代码:在create中,我们设置了初始开始位置、目标位置变量和鼠标点击变量。 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.释放鼠标后,它会检查苹果是否与 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.如果您尝试检查它们是否直接落在 obj_eight 的 x 和 y 上,将很难准确地落在该位置。

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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