1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Random spawned coin won't stop spawning in the wall

Discussion in 'Programming' started by Kevin_Sorbo, Mar 10, 2018.

  1. Kevin_Sorbo

    Kevin_Sorbo Guest

    Hi Y'all!

    Having an issue getting my random spawning coin to stay away from the walls when it spawns. The below code is supposed to spawn ten coins only if they are not near anything. Would appreciate any help you can give. Thanks is advance!

    randomize();
    {repeat (10) {
    var ranx = random(room_width);
    var rany = random(room_height);

    if point_distance(other.x, other.y, ranx, rany) >= 40 {
    instance_create_layer(ranx, rany, "layer_player", obj_coin);
    } else {
    do {
    var ranx = random(room_width);
    var rany = random(room_height);
    } until point_distance(other.x, other.y, ranx, rany) >= 40;
    }
    }
    }
     
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,199
    Try
    Code:
    randomize();
    repeat (10) {
        // guess starting position
        var ranx = random(room_width);
        var rany = random(room_height); 
       
        // check collisions with all other object in the game
        with (all) {
            while (point_distance(x, y, ranx, rany) < 40) {
                ranx = random(room_width);
                rany = random(room_height); 
            }
        }
       
        // spawn coins
        instance_create_layer(ranx, rany, "layer_player", obj_coin);
    }
    
    btw, you don't have to call randomize() everytime you do something random, you can call it at the start of the game once.
     
  3. Kevin_Sorbo

    Kevin_Sorbo Guest

    Thanks so much for the help! Sorry bit of a noob here, the coins are still not avoiding the walls. It there anything I could be missing?

    FYI I am placing this in the creation code of a non-visible coin spawning object. Do you think that could be a reason why it seams like part of the code is getting ignored or could it be something with my wall objects? Would really appreciate any help

    I think I may have been staring at the screen too long *__*
     
  4. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,199
    hmm, How big are the wall objects, if they're bigger than 40 pixels wide or tall it still can spawn inside.
    I assume all the walls are there before this code happens.
     
  5. Kevin_Sorbo

    Kevin_Sorbo Guest

    The walls are 64x64 so ~32 should be their clearance distance.

    I will check on the order of creation to make sure this is not messing it up. Will let you know how it goes. Thanks again!
     
  6. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,199
    Depends, if the origin of the walls is not in the middle then the longest distance would be 90.
    The longest distance for a centered origin is technically ~45 because point_distance counts with vectors.
    upload_2018-3-10_16-25-41.png

    And then you also have the size of your coins that work against you.
     

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