Javascript中的随机重复函数
我想生成我的函数Robot.prototype.seDeplacer的随机重复 这个函数给我的机器人一个随机移动(坐标),但我想创建一个随机移动的随机重复。 怎么可能呢 我不想要计时器之类的东西,只要重复我的Robot.prototype.seDeplacer函数的1、2或3次随机移动即可 我希望我已经够清楚了 多谢各位Javascript中的随机重复函数,javascript,random,coordinates,Javascript,Random,Coordinates,我想生成我的函数Robot.prototype.seDeplacer的随机重复 这个函数给我的机器人一个随机移动(坐标),但我想创建一个随机移动的随机重复。 怎么可能呢 我不想要计时器之类的东西,只要重复我的Robot.prototype.seDeplacer函数的1、2或3次随机移动即可 我希望我已经够清楚了 多谢各位 // Objet Robot function Robot(nick, pv, maxSpeed, position) { this.nick = nick; this.p
// 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();
});
如果我理解的话,你只需要一个有随机极限的循环:
var limit = Math.floor(Math.random() * 3) + 1; // 1, 2, or 3
for (var x = 0; x < limit; ++x) {
// Call your function
}
如果我理解的话,你只需要一个有随机极限的循环:
var limit = Math.floor(Math.random() * 3) + 1; // 1, 2, or 3
for (var x = 0; x < limit; ++x) {
// Call your function
}
我认为您仍然需要使用setInterval,但仍然可以使其随机触发:
setInterval(function () {
if(Math.floor(Math.random() * 255) + 1 > 127) {
robots.forEach(function (robot) {
robot.seDeplacer();
});
}
}, 250);
只有当Math.random()函数返回的值大于127时,这种情况才会随机发生。我认为您仍然需要使用setInterval,但仍然可以使其随机触发:
setInterval(function () {
if(Math.floor(Math.random() * 255) + 1 > 127) {
robots.forEach(function (robot) {
robot.seDeplacer();
});
}
}, 250);
只有当Math.random()函数返回的值大于127时,才会随机发生这种情况。首先,获取移动的“类型”并保存它:
var movement = Math.random();
然后将您的“Deplacer”代码包装成一个循环:
var i, l;
for (i = 0, l = Math.round(Math.random() * 3); i < l; i += 1) {
if (movement < 0.5) {
// mouvement sur l'axe x
} else {
// mouvement sur l'axe y
}
}
var i,l;
对于(i=0,l=Math.round(Math.random()*3);i
首先获取移动的“类型”并保存它:
var movement = Math.random();
然后将您的“Deplacer”代码包装成一个循环:
var i, l;
for (i = 0, l = Math.round(Math.random() * 3); i < l; i += 1) {
if (movement < 0.5) {
// mouvement sur l'axe x
} else {
// mouvement sur l'axe y
}
}
var i,l;
对于(i=0,l=Math.round(Math.random()*3);i
只是一个简单的循环,您当然可以移动到单独的函数以使其更干净
//Objet机器人
功能机器人(尼克、pv、maxSpeed、位置){
this.nick=nick;
这个.pv=pv;
this.maxSpeed=maxSpeed;
这个位置=位置;
};
随机函数(最大值、最小值){
返回parseInt((Math.random()*(max-min+1)),10)+min;
}
//机器人表达方法
Robot.prototype.sePresenter=函数(){
控制台。日志(“Bunjou-Je M'Appel'”+ Nek.+“jaaI”+ this。
};
Robot.prototype.seDeplacer=函数(){
if(Math.random()>0.5){
//阿克斯x运动测量
}否则{
//阿克斯伊运动测量
}
var dx=(Math.random()*this.maxSpeed*2)-this.maxSpeed;
此.position[0]+=dx;
console.log(“J'avancer vers”+此位置)
};
//变量数组
var机器人=[
新机器人('Maurice',95,2,5,8]),
新机器人('Lilian',76,3,[12,25]),
新机器人('Ernest',100,1[11,14]),
新机器人('Juliette',87,3,2,17]),
];
//团块
机器人。forEach(功能(机器人){
robot.sePresenter();
对于(var i=0;i });代码>只是一个简单的循环,您当然可以移动到单独的函数以使其更干净
//Objet机器人
功能机器人(尼克、pv、maxSpeed、位置){
this.nick=nick;
这个.pv=pv;
this.maxSpeed=maxSpeed;
这个位置=位置;
};
随机函数(最大值、最小值){
返回parseInt((Math.random()*(max-min+1)),10)+min;
}
//机器人表达方法
Robot.prototype.sePresenter=函数(){
控制台。日志(“Bunjou-Je M'Appel'”+ Nek.+“jaaI”+ this。
};
Robot.prototype.seDeplacer=函数(){
if(Math.random()>0.5){
//阿克斯x运动测量
}否则{
//阿克斯伊运动测量
}
var dx=(Math.random()*this.maxSpeed*2)-this.maxSpeed;
此.position[0]+=dx;
console.log(“J'avancer vers”+此位置)
};
//变量数组
var机器人=[
新机器人('Maurice',95,2,5,8]),
新机器人('Lilian',76,3,[12,25]),
新机器人('Ernest',100,1[11,14]),
新机器人('Juliette',87,3,2,17]),
];
//团块
机器人。forEach(功能(机器人){
robot.sePresenter();
对于(var i=0;i });代码>“我不想要计时器或类似的东西…”“我不想要计时器或类似的东西…”是的,我也是这样理解这个问题的。很确定这就是OP想要的。是的,我也是这样理解这个问题的。很确定这就是OP想要的。