[HELP]How to control ONLY one object when u have X objects on your background

Discussion in 'Programming' started by molikayess, Nov 16, 2017.

  1. molikayess

    molikayess Member

    Joined:
    Nov 15, 2017
    Posts:
    9
    Hi guys , i want to control only one object at the same time , because i created a board game , and each player have 4 pieces . It is possible on GMS to click with the mouse on a piece , to control only this piece and move it when you control it ?
    I tried with this code at first , without the mouse , with vk_enter :
    Code:
    right = keyboard_check_pressed(vk_right);
    left = keyboard_check_pressed(vk_left);
    up = keyboard_check_pressed(vk_up);
    down = keyboard_check_pressed(vk_down);
    
    
    
    vitesse = 25;
    
    
    
    while(keyboard_check_pressed(vk_enter)){
    
    
    
    if right{
    object11.x += vitesse;
    }
    
    if left{
    object11.x -= vitesse;
    }
    
    if up{
    object11.y -= vitesse;
    }
    
    if down{
    object11.y += vitesse;
    }
    }
    If u can help me it will be wonderful !
    Sorry for my english guys !
     
  2. Noam Shafir

    Noam Shafir Member

    Joined:
    Oct 11, 2017
    Posts:
    37
    You can use a mouse button event.
    I got confused of this in the start too. The regular mouse button event is to check whether the mouse button is held on the object, and the global mouse button event is to check whether the mouse button is held, no matter where.
    So you can create a "Left Button" event and write something like the following code inside:
    Code:
    x = mouse_x;
    
    You can also set up a snap to grid logic if your board game is grid based.
    I hope you found this reply useful.
     
    molikayess likes this.
  3. molikayess

    molikayess Member

    Joined:
    Nov 15, 2017
    Posts:
    9
    Thanks you dude , but now i've an other problem i wrote this :
    Code:
    object11.x = mouse_x
    object11.y = mouse y 
    And my piece move only on right and down . I cant move my object to the left or up it .

    And how can i use the grid logic ? Where can i put it ?
     
  4. Noam Shafir

    Noam Shafir Member

    Joined:
    Oct 11, 2017
    Posts:
    37
    For your first question: Isn't the object on the mouse pointer when you hold the mouse button?
    You have a few typos in the code you posted, so if you didn't rewrite it and just copied it from your game's code, put a _ between the "mouse" and "y", you should also put a ; at the end of each command, and you don't need to mention object11 before a variable unless you are calling the variable from a different object.
    For the snap to grid logic: (Edit: Sorry I gave you the wrong code)
    In the mouse button event, you need to write the following code instead of the code you have right now:
    Code:
    x = (mouse_x div grid_cell_width)*grid_cell_width;
    y = (mouse_y div grid_cell_height)*grid_cell_height;
    
    // If your object's origin point is the center of the sprite:
    x = (mouse_x div grid_cell_width)*grid_cell_width+grid_cell_width/2;
    y = (mouse_y div grid_cell_height)*grid_cell_height+grid_cell_height/2;
    
    That should do it.
     
    Last edited: Nov 19, 2017
  5. molikayess

    molikayess Member

    Joined:
    Nov 15, 2017
    Posts:
    9
    When i used this code
    Code:
    obj_eydon.x = mouse_x
    obj_eydon.y = mouse_y
    The sprite follow my mouse when i click on the sprite

    When i used your code , the sprite move only one time , after one click .

    So , i tried this and i've the same problem , i can only move my sprite on the right or down . My background grid has 17 times my cell . Its a 1088 width and 1088 height.
    One cell = 64 widht and 64 height , and my sprite are 50 widht and 50 height .
    At first my center of my sprite was (0,0), i tried and i had the same problem . I changed the center into (25,25) , and the problem still persist .
    I cant move my sprite on the left or up it and i don't know why :/
     
    Last edited: Nov 17, 2017
  6. Noam Shafir

    Noam Shafir Member

    Joined:
    Oct 11, 2017
    Posts:
    37
    1. I edited my previous reply.
    2. Maybe look for another code in your game that's bugging your movement, because I don't see any reason why my code should not work...
     
  7. Isabella Ava

    Isabella Ava Member

    Joined:
    Aug 5, 2016
    Posts:
    28
    First declare Global Variable in your first creating object's Create Event:
    globalvar eg_selected;
    then put these codes in the pieces Step Event
    if position_meeting(mouse_x,mouse_y,self) && mouse_check_pressed(mb_left)
    {
    x = mouse_x; y = mouse_y;
    eg_selected = id; //get ID of the last selected piece
    }
    //SNAP TO GRID
    x = (x div 64)*64 + 32;
    y = (y div 64)*64 + 32;
     
    Last edited: Nov 19, 2017

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice