jeudi 16 février 2017

How can i make two random numbers keep a certain distance away from each other?

I'm new to javascript programming but i am aware of how messy my code is, so i'm sorry for that. What Im trying to do is create 2 (for now, final version will have many) "Trees" that are just rectangles for now place on a random x value. But I don't want them to be within 40 pixels of eachother. I've been trying to write something that should achieve this but I can't figure out why it wont work, trees are still spawning on top of eachother! It's driving me crazy. I am using p5js and sublime text 2 by the way.

function bg(){


var tree = Math.floor(Math.random() * (380 - 0) + 00);
var tree2 = Math.floor(Math.random() * (380 - 0) + 0);
redefine(tree,tree2);
this.x=0;

this.show = function(){
noStroke();

//tree
fill(102, 51, 0);
rect(this.x+tree , 450, 26, 110);

//tree2
fill(102, 51, 0);
rect(this.x+tree2, 410, 26, 150);   
    }
}

function redefine(first, second){
if(second<=first-40 || second>=first+40){
console.log("good");
} else {
console.log("redefining")
second = Math.floor(Math.random() * (380 - 0) + 0);
    }
}

//key
// Math.random() * (max - min) + min    




Aucun commentaire:

Enregistrer un commentaire