How to move all objects together with their layer

Discussion in 'Programming' started by Jaydabblju, Oct 12, 2019 at 3:44 PM.

  1. Jaydabblju

    Jaydabblju Member

    Joined:
    Saturday
    Posts:
    3
    I am new to GM2 and tried to move a whole layer. But the instances of this layer stay on their position.

    I tried:
    Code:
    // creating buttons
    instance_create_layer(0,0,"menu",obj_btn_new_game);
    instance_create_layer(0,100,"menu",obj_btn_load_game);
    
    // moving the whole layer down
    layer_y("menu",200);
    
    But the objects don't move.

    My target is to create a menu on a layer. After clicking on a button of that menu the whole layer shall move out of the screen.
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,547
    Get the layer ID and use that to filter through all instances.
    Code:
    var menu_layer = layer_get_id("menu");
    with (all) {
      if (layer == menu_layer) {
        y += 200;
      }
    }
    
    See: with block recipe cards
     
  3. Jaydabblju

    Jaydabblju Member

    Joined:
    Saturday
    Posts:
    3
    So moving the layer doesn't affect the postition of its instances? I have to go through all instances manually?

    For what can I use "layer_y" and "layer"x" if the instances don't move?
     
  4. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,703
    The with loop FrostyCat posted will move all object instances, while technically being "manually going through all instances" it's just 6 lines of code to do it (because we tell it to loop over all objects instead of just specific objects or a specific ID).
     
  5. Jaydabblju

    Jaydabblju Member

    Joined:
    Saturday
    Posts:
    3
    Yes I get that.
    But again: For what can I move the layer with "layer_x" and "layer_y" if the instances on this layer stay on their positions?
     
  6. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    975
    Background images, etc., for parallax effects, and things like that?
     

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