# Distance between randomly spawned objects

**URL:** <https://discourse.processing.org/t/distance-between-randomly-spawned-objects/18026>\
**Category:** Libraries\
**Created:** [February 22, 2020, 9:05pm UTC](https://discourse.processing.org/t/distance-between-randomly-spawned-objects/18026 "2020-02-22T21:05:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kothal](https://avatars.discourse-cdn.com/v4/letter/k/e56c9b/32.png) [@Kothal](https://discourse.processing.org/u/Kothal)\
**Post date:** [February 22, 2020, 9:05pm UTC](https://discourse.processing.org/t/distance-between-randomly-spawned-objects/18026/1 "2020-02-22T21:05:03Z")

</div>

I’m a beginner who just started out learning js and p5. I started working on making my own game from scratch, it’s actually a replica of the chrome dino game, I managed to do everything I wanted to and I’m almost finished, but… its sort of unplayable, some times. The enemies that come from the right are spawned randomly, and sometimes they spawn kinda close, making it very difficult jumping over them without losing. And I don’t know how to make them spawn at a certain distance between them.  
Please let me know if I need to provide something else, i’m a newbie 🙂 my code from the main js file is below:

```auto
let d;
let bg;
let imgDino;
let imgEnemy;
let enemies = [];
let song;
let hitSound;
let s = 0;
let jSound;
let scoreSound;
let myint;
let gameover = false;
function preload() {
	bg = loadImage("resources2/backgrnd.jpg");
	imgDino = loadImage("resources2/dino.png");
	imgEnemy = loadImage("resources2/snakecrop.png");
	hitSound = loadSound("resources2/hit.wav");
	jSound = loadSound("resources2/jump.wav");
	scoreSound = loadSound("resources2/sound.mp3");

}
function setup() {
    createCanvas(1400, 400);
    d = new Dino();
    jSound.setVolume(0.5);
    myint = setInterval(score, 100);
}

function keyPressed() {
	if(key == ' ') {
		d.jump();
	}
	if(keyCode == DOWN_ARROW) {
		d.duck();
	}
	if(keyCode == 82) {
		location.reload();
	}
}

function draw() {
	background(bg);
		d.show();
	    d.move();
	    for(let e of enemies) {
		if(d.hits(e)) {
        e.endgame();
	}
		e.move();
		e.show();
	}
	//spawning an enemy
	if(random(1) < 0.005) {
		enemies.push(new Enemy());
	  }

        if(gameover == true) {
	    hitSound.play();
	    noLoop();
	    clearInterval(myint);
        loadGameOverInterface();

        }
        scoreDisplay();
}

function score() {
	s += 1;
	if(s == 200) {
		scoreSound.play();
	}
		if(s == 400) {
		scoreSound.play();
	}
		if(s == 500) {
		scoreSound.play();
	}
		if(s == 600) {
		scoreSound.play();
	}
		if(s == 700) {
		scoreSound.play();
	}
		if(s == 800) {
		scoreSound.play();
	}
		if(s == 1000) {
		scoreSound.play();
	}
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [February 23, 2020, 5:22am UTC](https://discourse.processing.org/t/distance-between-randomly-spawned-objects/18026/2 "2020-02-23T05:22:06Z")

</div>

Can you describe the rule that you want?

Is it like:

If there is no enemy closer than x pixels from the edge of the screen, check to sometimes spawn a new enemy

Or

If it has been at least x frames / seconds since the last enemy was spawned, check to sometimes spawn a new enemy

Or…?

---

<div class="post-metadata">

**Author:** ![Kothal](https://avatars.discourse-cdn.com/v4/letter/k/e56c9b/32.png) [@Kothal](https://discourse.processing.org/u/Kothal)\
**Post date:** [February 23, 2020, 6:40am UTC](https://discourse.processing.org/t/distance-between-randomly-spawned-objects/18026/3 "2020-02-23T06:40:59Z")

</div>

Something like that. An enemy spawns at the beginning of the game and the rest need to be spawned randomly, but the first one must not be closer than x pixels from the right edge of the canvas.  
I actually did something like this, but it was like: an enemy comes, i jump, another one comes, i jump, it wasn’t very challenging, although it was playable.  
The second option sounds good too, for ex: don’t spawn another enemy if the previous one hasn’t been on the screen for 1 second

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [February 24, 2020, 2:50pm UTC](https://discourse.processing.org/t/distance-between-randomly-spawned-objects/18026/4 "2020-02-24T14:50:04Z")

</div>

> [@jeremydouglass](#):
>
> If there is no enemy closer than x pixels from the edge of the screen, check to sometimes spawn a new enemy

Does that approach resolve your issue? Are you able to to add this kind of logic around here?

```
if(random(1) < 0.005) {

```

---

<div class="post-metadata">

**Author:** ![Kothal](https://avatars.discourse-cdn.com/v4/letter/k/e56c9b/32.png) [@Kothal](https://discourse.processing.org/u/Kothal)\
**Post date:** [February 24, 2020, 3:10pm UTC](https://discourse.processing.org/t/distance-between-randomly-spawned-objects/18026/5 "2020-02-24T15:10:31Z")

</div>

I don’t really know how… That if statement in my code spawns them randomly, sometimes I’m lucky and they spawn at good distance between each other, but sometimes, since it’s completely random, they spawn even 3 or 4 near each other.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [February 24, 2020, 5:15pm UTC](https://discourse.processing.org/t/distance-between-randomly-spawned-objects/18026/6 "2020-02-24T17:15:43Z")

</div>

Exactly. So add an extra if clause that checks the enemy location.

---

<div class="post-metadata">

**Author:** ![Kothal](https://avatars.discourse-cdn.com/v4/letter/k/e56c9b/32.png) [@Kothal](https://discourse.processing.org/u/Kothal)\
**Post date:** [February 29, 2020, 9:10pm UTC](https://discourse.processing.org/t/distance-between-randomly-spawned-objects/18026/7 "2020-02-29T21:10:29Z")

</div>

I forgot to respond :))  
I fixed it, thanks for the help. I managed to do something else, generating a minimum and maximum gap between the spawned enemy.
