Random spawned coin won't stop spawning in the wall

K

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;
}
}
}
 

Simon Gust

Member
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.
 
K

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 *__*
 

Simon Gust

Member
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 *__*
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.
 
K

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!
 

Simon Gust

Member
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!
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.
 
Top