【发布时间】:2015-06-30 12:44:28
【问题描述】:
我正在使用 Javascript 来构建我不起眼的机器人军队。
// 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);
};
//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();
});
我想添加机器人动作。每转一圈,机器人可以在 1 和它的 maxSpeed 之间移动一定数量的空间。每个动作都可以向上/向下/向左/向右。
我知道我必须使用Maths.random,但我无法解释机器人如何移动。
这里是函数的开始
Robot.prototype.seDeplacer = function() {
var point1X = (this.position(Math.random() * this.maxSpeed+1);
var point1Y = (this.position(Math.random() * this.maxSpeed)+1;
console.log("je suis" + point1X + point1Y);
};
robots.forEach(function(robot) {
robot.seDeplacer();
});
我在机器人运动的正确轨道上吗?
【问题讨论】:
-
那么问题到底是什么?你有什么问题?我猜
position应该是一个数组,所以更新你传递给seDeplacer的机器人中的位置。 -
请注意,
point1X和point1Y的声明编写方式不同。看看结尾,括号在“1”的哪一侧。
标签: javascript coordinates euclidean-distance