1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

issue with "other"

Discussion in 'Programming' started by Rick-V, Jan 13, 2018.

  1. Rick-V

    Rick-V Guest

    hello i'm pretty new on GMS and recently i found a problem:
    i was trying to make a movement like chess, here is the code, casella_movimento stay for "movement_slot"

    step event:
    Code:
    mb_left = mouse_check_button(mb_left);
    
    ///slots
    if(mov == true){
    instance_create(x+40,y,obj_casella_movimento);
    instance_create(x,y+40,obj_casella_movimento);
    instance_create(x-40,y,obj_casella_movimento);
    instance_create(x,y-40,obj_casella_movimento);
        mov = false;
    }
    slot collision event:
    Code:
    ///movement
    if(mb_left){
        x = other.x;
        y = other.y;
        mov = true;
    }
    the problem is that the player doesn't move at the slot's x, y.
    Could anybody help me please?
    thanks a lot!
     
  2. JFitch

    JFitch Member

    Joined:
    Sep 28, 2016
    Posts:
    428
    mb_left is a built in constant. You can't use it as a variable.
     
  3. Rick-V

    Rick-V Guest

    actually in my code i wrote mb_left in italian (mb_sinistra), so the built in constant is not the problem:(
     
  4. JFitch

    JFitch Member

    Joined:
    Sep 28, 2016
    Posts:
    428
    Code:
    if(mb_sinistra){
       x = other.x;
       y = other.y;
       mov = true;
    }
    Your code is saying "if the object calling this code is colliding with a slot and if the left mouse button is held with the mouse anywhere on the screen, move the object calling this code to the slot's position." Is that what you're trying to do?
     
  5. Rick-V

    Rick-V Guest

    thanks, i tried to fix it but it doesn't seem working anyway
    Code:
    ///movimento
    if(mb_sinistra){
        if(position_meeting(mouse_x,mouse_y,obj_casella_movimento)){
            x = other.x;
            y = other.y;
            mov = true;
        }
    }
     
  6. Paskaler

    Paskaler Member

    Joined:
    Jul 4, 2016
    Posts:
    317
    You'll have to get the ID of the instance:

    Code:
    ///movimento
    if(mb_sinistra){
      var inst = instance_place(mouse_x, mouse_y, obj_casella_movimento);
     if(instance_exists(inst)){
           x = inst.x;
           y = inst.y;
           mov = true;
       }
    }
    
    Also, other is only usable from within a with statement to target the instance outside the statement and in the collision event to target the instance the instance that called the event collided with.
     
    Rick-V likes this.
  7. Rick-V

    Rick-V Guest

    thanks! now it works perfectly!
    I have still a question: it's like the mask of the slot is moved from the center of the sprite(both sprites are centered)
     

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