// Protect the workers and animals!
// Defend these two positions:
var defend = [];
defend[0] = { x: 98, y: 28 };
defend[1] = { x: 84, y: 7 };
var soldiers = [];
var friends = hero.findFriends();
for(var i=0; i < friends.length; i++) {
var friend = friends[i];
if(friend.type == "soldier") {
soldiers.push(friend);
} else {
// Defend the workers:
defend.push(friend);
}
}
while(true) {
// Use a for-loop to assign each soldier to a corresponding defend[] target
// Use command(soldier, "defend", thang) or command(soldier, "defend", position)
for(var a = 0; a > soldiers.length; a++) {
var soldier = soldiers[a];
if (soldier) {
hero.command(soldier, "defend", defend[friends.length]);
}
}
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.attack(enemy);
}
}
struggling at the command soldier part and for loops part could someone explain me what wrong with my code
// Protect the workers and animals!
// Defend these two positions:
var defend = [];
defend[0] = { x: 98, y: 28 };
defend[1] = { x: 84, y: 7 };
var soldiers = [];
var friends = hero.findFriends();
for(var i=0; i < friends.length; i++) {
var friend = friends[i];
if(friend.type == "soldier") {
soldiers.push(friend);
} else {
// Defend the workers:
defend.push(friend);
}
}
while(true) {
// Use a for-loop to assign each soldier to a corresponding defend[] target
// Use command(soldier, "defend", thang) or command(soldier, "defend", position)
for(var a = 0; a > soldiers.length; a++) {
var soldier = soldiers[a];
if (soldier) {
hero.command(soldier, "defend", defend[a]);
}
}
var enemy = hero.findNearestEnemy();
if (enemy) {
hero.attack(enemy);
}
}