jeudi 16 juillet 2015

Random repetition function in Javascript

I would like to generate a random repetition of my function Robot.prototype.seDeplacer

this function give a random move to my robots (coordinates) but I want to create a random repetition of the random move. How is it possible?

I don't want a timer or something like that, just repeat 1, 2 or 3 times the random move from my Robot.prototype.seDeplacer function.

I hope I'm enough clear.

Thank you very much

// Objet Robot
function Robot(nick, pv, maxSpeed, position) {
 this.nick = nick;
 this.pv = pv;
 this.maxSpeed = maxSpeed;
 this.position = position;
};

//Méthode présentation des robots
Robot.prototype.sePresenter = function() {
 console.log("Bonjour je m'appelle " + this.nick + ". J'ai " + this.pv + " points de vie." + " Je me déplace à " + this.maxSpeed + " cases par seconde. Je suis à la case de coordonnées " + this.position);
};
Robot.prototype.seDeplacer = function() {
 if (Math.random() > 0.5) {
 // mouvement sur l'axe x
 } else {
 // mouvement sur l'axe y
 }
 var dx = (Math.random() * this.maxSpeed * 2) - this.maxSpeed;
 this.position[0] += dx;

 console.log("J'avancer vers " + this.position)
};

//Variables array
var robots = [
 new Robot('Maurice',95,2,[5,8]),
 new Robot('Lilian',76,3,[12,25]),
 new Robot('Ernest',100,1,[11,14]),
 new Robot('Juliette',87,3,[2,17]),
];

//boucle
 robots.forEach(function(robot) {
 robot.sePresenter();
 robot.seDeplacer();
});




Aucun commentaire:

Enregistrer un commentaire