2015-11-13 93 views
-1

如何將任何物體在遊戲製作工作室

我想光標移動對象的任何其他對象的位置移動到特定位置的光標(鼠標)。

在這段代碼中,蘋果對象正在移動一個遊標,但它並不停止在(八個對象)。在其他情況下,如果我將Apple對象放在除了Eight-Object以外的任何其他位置,它必須返回到開始位置。

//步驟事件

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; 
} 
} 

回答

0

我試着去了解你的目標是什麼。如果我正確讀取它,您正在嘗試使用鼠標將項目移動到另一個項目,並且如果它們放在不是目標位置的位置,它應該返回到其原始位置。如果這是正確的,代碼來執行,這將是類似於下面:

**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; 

運行通過代碼: 在創建中,我們設置的初始起始位置,目標位置的變量,並用鼠標點擊變量。如果該對象正在點擊,它將保持爲真,並隨着鼠標移動,直到釋放鼠標。鼠標釋放後,它會檢查蘋果是否與obj_eight發生碰撞。如果你試圖檢查它們是否直接落在obj_eight的x和y上,它將極其難以準確地落在該點上。

希望這會有幫助