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

GMS 2 simple maths (I think) * SOLVED *

Discussion in 'Programming' started by Mallard8, Mar 23, 2018.

  1. Mallard8

    Mallard8 Guest

    I have a room 3200 width
    10 objects 160 width
    How can I get them evenly spaced across the room width

    3200 - 160 x 10 = 1600 / 11 = Place object every 145.45

    I think this is what I need but how to code it correctly?
     
  2. DarthTenebris

    DarthTenebris Definitely not a Sith Lord

    Joined:
    Jun 22, 2016
    Posts:
    497
    With the power of maths, to space n objects which have a width of w each evenly across a length of L, you would need to space them:
    Code:
    var space = (L - (n * w)) / (n - 1)
    Hope I helped :)

    EDIT:
    To place your objects:
    Code:
    xx = 0;
    for (i = 0; i < n; i++) {
         instance_create(xx + space, y, object);
         xx += space;
    }
    
     
    Last edited: Mar 23, 2018
  3. Geoff Jones

    Geoff Jones Member

    Joined:
    Aug 20, 2016
    Posts:
    499
    A loop that places the instance
    Code:
    var xdist=(room_width-(160x10)) / 11
    var xstart
    var i
    for (i=0 ; i<10 ; i++){
          instance_create_layer(xdist+xstart, y, "layer name", object_name)
          xstart+=xdist
    }
    
     
  4. JFitch

    JFitch Member

    Joined:
    Sep 28, 2016
    Posts:
    428
    Look up the "lerp" function.
     
  5. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    That won't work.

    You calculated "space" to be the distance between the rightmost edge and the leftmost edge of two subsequent instances.
    You did this under the assumption that the leftmost edge of the first instance would be placed at x = 0
    and the rightmost edge of the last instance would be placed at x = L.
    It may be that Mallard8 wanted this, but my guess is they don't.
    They probably wanted to have the same amount of space to the left of the first instance and to the right of the last instance as there is inbetween 2 subsequent instances.

    Anyway, the loop you then use treats "space" as though it was defined as the distance between the origins of 2 subsequent instances.
    It also assumes the origin of the sprite of those instances are placed at the left edge of the sprite.

    As a final detail, instance_create doesn't work with GM:S 2.

    Same main mistake, you define "xdist" to be something different from the way you use it.
    On top of that, xstart is a built-in variable, so you cannot declare it to be in a local scope through "var".
    I also don't think you can pass a string as layer id to instance_create_layer.
    EDIT: You can pass a string there.

    Would work.
    I wouldn't personally use it though.

    Same main mistake as the guys above.

    Let's call the room width "L", the instance width "W", the amount of instances "N" and the sprite offset of your instances "OX",
    the y-coordinate at which the instances should get spawned "Y", the id of the layer on which the instances sould get spawned "LAYER" and the object index of the instances "OBJ".
    You'll want to calculate the "space" between consecutive instances as done above.
    Then you'll want to do tis with it:
    Code:
    var space = (L - W * N) / (N + 1)
    var dx = space + W;
    for(var k = 0; k < N; ++k) {
        instance_create_layer(space + OX + dx * k, Y, LAYER, OBJ);
    }
    EDIT: fixed mistake
     
    Last edited: Mar 31, 2018
    rIKmAN, Geoff Jones and Mallard8 like this.
  6. Mallard8

    Mallard8 Guest

    Tried the above but for some reason I get a row of dragons instead of cages?
    Code:
    /// @description Draw cages on screen
    var L = room_width; // This initial x position to draw from
    var Y = sprite_get_height(spr_Cage)/2 // The initial y position to draw from
    var W = sprite_get_width(spr_Cage); // Set sprite to whatever is required
    var N = 10;
    var OX = 10;
    
    
    var dx = (L - W) / (N + 1);
    for(var k = 0; k < N; ++k) {
        instance_create_layer(OX + dx * k, Y,"rm_cages",spr_Cage);
    }
    
     
  7. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    My bet is that "spr_Cage" is not an object, but a sprite.
    instace_create_layer expects an object index, so it got confused by you giving it a sprite index.
    To add sprites, you'll need to use layer_sprite_create(LAYER, OX + dx * i, Y, SPR) instead.
     
    Mallard8 likes this.
  8. Mallard8

    Mallard8 Guest

    Thanks for all the relies special thanks to jo-thijs who pointed me in the right direction and yes obj_Cage doesn't have a sprite so I was trying to use spr_cage.
    I ended up with the code below which works a treat.
    Code:
    /// @description Draw cages on screen
    var rm_len = room_width;
    var rm_hgt = sprite_get_height(spr_Cage)/2 ;
    var sp_wide = sprite_get_width(spr_Cage); 
    var ox = 300; // Sprite offset
    var sp_num = 10; // Number of cages needed
    
    var dx = (rm_len - sp_wide) / (sp_num + 1);
    
    for (var i = 0; i < n; ++i;)
    {
    
    if dragon_array[i] > -1 // Check to see if a dragon has been captured
        {
        draw_sprite(dragon_array[i], 0,ox + dx *i, rm_hgt); // If dragon is caught place it in to a cage
        }
    draw_sprite(spr_Cage, 0, ox + dx * i, rm_hgt); // Draw a row of cages along top of screen
    
    }
    
     
  9. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    Actually, I made a mistake, resulting in an uneven space distribution.
    I edited my comment to fix the mistake.
     
    Mallard8 likes this.
  10. Mallard8

    Mallard8 Guest

    Thanks for the edit .
     

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